Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failure to decode project description generated by swiftpm. #214

Closed
2 tasks done
kielgillard opened this issue Jul 20, 2021 · 5 comments · Fixed by #215
Closed
2 tasks done

Failure to decode project description generated by swiftpm. #214

kielgillard opened this issue Jul 20, 2021 · 5 comments · Fixed by #215
Assignees
Labels
good first issue Good for newcomers installer bug Breaks the installation proccess reproducible It’s not just you; we see it too!

Comments

@kielgillard
Copy link
Contributor

New Issue Checklist

Description

Mockingbird cannot parse the json description of my Swift package.

error: keyNotFound(CodingKeys(stringValue: "dependencies", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "targets", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"dependencies\", intValue: nil) (\"dependencies\").", underlyingError: nil))

redacted-desc.json.zip (I redacted some local paths).

mockingbird generate --project redacted-desc.json --target CompanyTextInput --support MockingbirdSupport 

Environment

0.16.0 / Big Sur / Installed with Swift Package Manager / XCTest / Uses .mockingbird-ignore / Uses supporting files

Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57)
Target: x86_64-apple-darwin20.5.0

  • Mockingbird CLI version (mockingbird version)
  • Xcode and macOS version (are you running a beta?)
  • Swift version (swift --version)
  • Installation method (CocoaPods, Carthage, from source, etc)
  • Unit testing framework (XCTest, Quick + Nimble, etc)
  • Does your project use .mockingbird-ignore?
  • Are you using supporting source files?
@kielgillard
Copy link
Contributor Author

kielgillard commented Jul 20, 2021

Ultimately, I'm trying to generate mocks the sources of my package for the package tests. I do not have an Xcode project. So there'd be a generate-mocks.sh script in the package developers would run to update the mocks:

#!/bin/bash
set -eu

package_name=${PWD##*/}
echo "Package name: ${package_name}"

package_desc="${package_name}.json"
echo "Package desc name: ${package_desc}"

if [[ -d $package_desc ]]; then
  echo "Aborting because a JSON description already exists at ${package_desc}"
  exit 1
fi

cleanup() {
  rm -rf "${package_desc}"
  rm -rf MockingbirdSupport
}
trap cleanup EXIT

swift package describe --type json > "${package_desc}"
echo "Described package. Downloading..."

# Commenting out for now, folder manually installed.
# mockingbird download starter-pack

echo "Generating mocks"

mockingbird generate \
  --project "${package_desc}" \
  --target "${package_name}" \
  --output "Tests/${package_name}Mocks.generated.swift" \
  --support MockingbirdSupport

@andrewchang-bird
Copy link
Contributor

andrewchang-bird commented Jul 20, 2021

Thanks for reporting. Originally when JSON project descriptions were added in #171 the intent was to upstream some changes to SPM, but it looks like they’ve since added target_dependencies as part of the project description output. It should be a minor change to get this to work.

In the meantime, you can work around this by post-processing the package description output by SPM, replacing all occurrences of "target_dependencies" with "dependencies". Or, simply manually generate and then modify each target.

@kielgillard
Copy link
Contributor Author

Thanks! Works a treat. I also get the following decoding error with this JSON, too:

error: keyNotFound(CodingKeys(stringValue: "dependencies", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "targets", intValue: nil), _JSONKey(stringValue: "Index 1", intValue: 1)], debugDescription: "No value associated with key CodingKeys(stringValue: \"dependencies\", intValue: nil) (\"dependencies\").", underlyingError: nil))

Target in question:

    {
      "c99name" : "KeychainAdditions",
      "module_type" : "SwiftTarget",
      "name" : "KeychainAdditions",
      "path" : "Sources/KeychainAdditions",
      "product_memberships" : [
        "KeychainAdditions"
      ],
      "sources" : [
        "AppleCredentialStorageService.swift",
        "KeychainItem.swift"
      ],
      "type" : "library"
    }

Post-processing the JSON description for this target with dependencies: [] works as expected and generates the mocks.

@kielgillard
Copy link
Contributor Author

@andrewchang-bird in my adventures I see that swift package describe --type json and swift package dump-package produce inconsistent results, where the describe command does not include target dependencies. The PR I submitted increases Mockingbird's dependency on the describe command. Should I submit a PR to parse JSON from dump-package instead?

@kielgillard
Copy link
Contributor Author

Submitted #216

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers installer bug Breaks the installation proccess reproducible It’s not just you; we see it too!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants