Brightify / Cuckoo

Boilerplate-free mocking framework for Swift!
MIT License
1.67k stars 174 forks source link

Problem with run script on Xcode 15 #484

Closed karolbielski closed 10 months ago

karolbielski commented 10 months ago

I am facing two problems:

  1. "No Cuckoo Generator found." error.
  2. "Could not read contents of ..." error.

Run script output

When I try to build test target I have:

Generated Mocks File = /Users/kb/Documents/Software-Developer/Experiment-Learn/WeatherApp/WeatherApp/WeatherAppTests/GeneratedMocks.swift
Mocks Input Directory = /Users/kb/Documents/Software-Developer/Experiment-Learn/WeatherApp/WeatherApp/WeatherApp
Script path: /Users/kb/Library/Developer/Xcode/DerivedData/WeatherApp-hiyidxszgimrtofxkwaxouhrgpyx/SourcePackages/checkouts/Cuckoo
Performing clean build.
No Cuckoo Generator found.
~/Library/Developer/Xcode/DerivedData/WeatherApp-hiyidxszgimrtofxkwaxouhrgpyx/SourcePackages/checkouts/Cuckoo ~/Documents/Software-Developer/Experiment-Learn/WeatherApp/WeatherApp
Downloading latest version...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  3627  100  3627    0     0  10911      0 --:--:-- --:--:-- --:--:-- 10891
Downloading Cuckoo Generator from URL: https://github.com/Brightify/Cuckoo/releases/download/1.10.4/cuckoo_generator
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

 86 7750k   86 6695k    0     0  9078k      0 --:--:-- --:--:-- --:--:-- 9078k
100 7750k  100 7750k    0     0   9.9M      0 --:--:-- --:--:-- --:--:-- 51.4M
~/Documents/Software-Developer/Experiment-Learn/WeatherApp/WeatherApp
Could not read contents of `/Users/kb/Documents/Software-Developer/Experiment-Learn/WeatherApp/WeatherApp/WeatherApp/Service/Forecast/ForecastService.swift`
FileKitError(Could not write to file at "/Users/kb/Documents/Software-Developer/Experiment-Learn/WeatherApp/WeatherApp/WeatherAppTests/GeneratedMocks.swift")
Command PhaseScriptExecution failed with a nonzero exit code

My configuration

I added Cuckoo using SPM to my test target and created run script also in test target and put it above "Compile Sources":

Zrzut ekranu 2024-01-16 o 22 08 52

"Generate Fakes" run script:

if [ $ACTION == "indexbuild" ]; then
  echo "Not running Cuckoo generator during indexing."
  exit 0
fi

# Skip for preview builds
if [ "${ENABLE_PREVIEWS}" = "YES" ]; then
  echo "Not running Cuckoo generator during preview builds."
  exit 0
fi

# Define output file. Change "${PROJECT_DIR}/${PROJECT_NAME}Tests" to your test's root source folder, if it's not the default name.
OUTPUT_FILE="${PROJECT_DIR}/${PROJECT_NAME}Tests/GeneratedMocks.swift"
echo "Generated Mocks File = ${OUTPUT_FILE}"

# Define input directory. Change "${PROJECT_DIR}/${PROJECT_NAME}" to your project's root source folder, if it's not the default name.
INPUT_DIR="${PROJECT_DIR}/${PROJECT_NAME}"
echo "Mocks Input Directory = ${INPUT_DIR}"

# Generate mock files, include as many input files as you'd like to create mocks for.
"${BUILD_DIR%/Build/*}/SourcePackages/checkouts/Cuckoo/run" --clean --download generate --testable "${PROJECT_NAME}" \
--output "${OUTPUT_FILE}" \
"${INPUT_DIR}/Service/Forecast/ForecastService.swift"
# ... and so forth, the last line should never end with a backslash

# After running once, locate `GeneratedMocks.swift` and drag it into your Xcode test target group.

File manager of the project with file to be generated:

Zrzut ekranu 2024-01-16 o 22 18 41

I added --clean --download to Cuckoo/run script as suggested in other Issues but with no luck.

I am using Xcode 15.1, Cuckoo 1.10.4.

Should I do something additional to help generator to be found?

About "Could not read contents of ..." error: the file at path "/Users/kb/Documents/Software-Developer/Experiment-Learn/WeatherApp/WeatherApp/WeatherApp/Service/Forecast/ForecastService.swift" exists and the path is correct.

I would be happy if someone help me solve this. Thanks in advance.

MatyasKriz commented 10 months ago

Hey, I think the main problem is that the Generator is unable to write to the generated file. Does the directory at /Users/kb/Documents/Software-Developer/Experiment-Learn/WeatherApp/WeatherApp/WeatherAppTests/GeneratedMocks.swift exist? Perhaps try creating the GeneratedMocks.swift file and see if that fixes it.

karolbielski commented 10 months ago

Yes, folder at path /Users/kb/Documents/Software-Developer/Experiment-Learn/WeatherApp/WeatherApp/WeatherAppTests exists. I created GeneratedMocks.swift file manually but the result is the same as before.

MatyasKriz commented 10 months ago

Hm, I see. The No Cuckoo Generator found. is not an error, it's simply an info for you that a generator will need to be either built from sources or downloaded, the latter of which the run script promptly does.

Regarding the FileKit error, it seems the generator has issues both reading the input file and writing the output file, so if the paths are absolutely correct, this hints at a permission issue. I know that recent Mac versions started requiring Documents access permission to read or write, so that might be it.

karolbielski commented 10 months ago

I managed to solve the issue.

All paths and files are correct. But the errors "Could not read contents of ..." and "Could not write to file at ..." gave me a hint that it is a problem with reading and writing to files that are input and output of the script.

It turns out that from Xcode 15 value of ENABLE_USER_SCRIPT_SANDBOXING (reference) in Build Settings is Yes by default. As far as I understood, it means that if there is no input or output arguments to run script (declared in Xcode), the system sandboxes the script and as a result input/output files cannot be read/write from inside the script.

After I change ENABLE_USER_SCRIPT_SANDBOXING to No the script started to work as expected and GeneratedMocks.swift file was generated.

@MatyasKriz Thanks for your time and effort. I'd be happy to create a PR that adds information about this problem to installation instructions in README.

MatyasKriz commented 10 months ago

Oh yeah, I remember something about this and I thought it was already in README, but it may have been in an issue or somewhere else entirely then.

If you'd be so kind, I'd happily merge this addition so others don't have to battle with this. 🙂

karolbielski commented 10 months ago

README is updated. I am closing the issue.