tuist / XcodeProj

πŸ“ Read, update and write your Xcode projects

Home Page:https://xcodeproj.tuist.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BuildPhaseTests not handling failure cases properly

tjwio opened this issue Β· comments

Context πŸ•΅οΈβ€β™€οΈ

#596 for context, tip of main

What 🌱

Many of the _____BuildPhaseTests aren't properly handling failure cases.
For example many of the tests look similar to this, which ensures that the decoder will throw

func test_init_fails_whenFilesIsMissing() {
        var dictionary = testDictionary()
        dictionary.removeValue(forKey: "files")
        let data = try! JSONSerialization.data(withJSONObject: dictionary, options: [])
        let decoder = XcodeprojJSONDecoder()
        do {
            _ = try decoder.decode(PBXCopyFilesBuildPhase.self, from: data)
            XCTAssertTrue(false, "Expected to throw an error but it didn't")
        } catch {}
    }

This seems to suggest that removing the key "files" causes the decoder to throw. However, this is not the case -- the testDictionary and JSONDecoder (namely decodeIntIfPresent see #596) will always throw no matter what because it encodes dstSubfolderSpec as an Int even though decodeIntIfPresent expects a string to be encoded.

func testDictionary() -> [String: Any] {
        [
            "dstPath": "dstPath",
            "buildActionMask": 0,
            "dstSubfolderSpec": 12,
            "files": ["a", "b"],
            "runOnlyForDeploymentPostprocessing": 0,
            "reference": "reference",
        ]
    }

Proposal πŸŽ‰

My proposed solution is two fold:

  1. Fix decodeIntIfPresent to try to decode an int if possible (ie. if decoding as a string then boolean fails).
  2. Fix tests to check correct failure case (ie. in a lot of these it's checking for default or nil values).

PR here: #598

Fixed in #598