typealiased / mockingbird

A Swifty mocking framework for Swift and Objective-C.
https://mockingbirdswift.com
MIT License
656 stars 81 forks source link

Failure to decode project description generated by `swiftpm`. #214

Closed kielgillard closed 3 years ago

kielgillard commented 3 years ago

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

kielgillard commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

Submitted https://github.com/birdrides/mockingbird/pull/216