BuildPhaseTests not handling failure cases properly
tjwio opened this issue Β· comments
Tim Wong commented
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:
- Fix
decodeIntIfPresent
to try to decode an int if possible (ie. if decoding as a string then boolean fails). - Fix tests to check correct failure case (ie. in a lot of these it's checking for default or nil values).
PR here: #598