import Testing @testable import ParcelDev struct CheckToolsTests { @Test func safetyChecksIgnoreCommentsAndHonorOptionalFailureReasons() throws { let source = """ // try? ignored(); value as! Type let value = try? operation() // swift-safety: allow-try? -- optional fixture let retry = retry?() let first = try? operation(); let second = try ? operation() // swift-safety: allow-try? -- intentional """ #expect(try CheckTools.safetyFailures(source: source, path: "Sources/Fixture.swift").isEmpty) } @Test func safetyChecksReportEveryUnsafeLineWithCorrectLineNumbers() throws { let source = "// café\r\nlet a = try? operation()\r\nlet b = object as! Value // swift-safety: allow-try? -- not a cast allowance\r\n" let failures = try CheckTools.safetyFailures(source: source, path: "Sources/Fixture.swift") #expect(failures.count == 2) #expect(failures[0].hasPrefix("Sources/Fixture.swift:2:")) #expect(failures[1].hasPrefix("Sources/Fixture.swift:3:")) } @Test(arguments: [ "let a = try? operation() // swift-safety: allow-try? -- ", "let a = try? operation() // swift-safety: allow-try? --", "let a = try? operation(); let b = a as! Value // swift-safety: allow-try? -- reason", "let a = try ? operation()", ]) func safetyChecksRejectMissingReasonsAndCasts(source: String) throws { #expect(try CheckTools.safetyFailures(source: source, path: "Sources/Fixture.swift").count == 1) } @Test func catalogFillsOnlyMissingSourceTranslations() throws { let translated = DevJSON.object([ "stringUnit": .object(["state": .string("translated"), "value": .string("Existing")]) ]) let catalog = DevJSON.object([ "sourceLanguage": .string("en"), "version": .string("1.0"), "strings": .object([ "Café": .object(["comment": .string("Preserved")]), "Translated": .object(["localizations": .object(["en": translated, "fr": translated])]), "French only": .object(["localizations": .object(["fr": translated])]), ]), ]) let prepared = try CheckTools.preparedCatalog(catalog) #expect(prepared["strings"]?["Café"]?["localizations"]?["en"]?["stringUnit"]?["value"] == .string("Café")) #expect(prepared["strings"]?["Café"]?["comment"] == .string("Preserved")) #expect(prepared["strings"]?["Translated"]?["localizations"]?["en"] == translated) #expect(prepared["strings"]?["French only"]?["localizations"]?["fr"] == translated) #expect( prepared["strings"]?["French only"]?["localizations"]?["en"]?["stringUnit"]?["state"] == .string("translated")) #expect(prepared["version"] == .string("1.0")) #expect(try CheckTools.preparedCatalog(prepared) == prepared) } @Test func catalogRejectsMalformedInput() { let fixtures: [DevJSON] = [ .array([]), .object([:]), .object(["sourceLanguage": .string("en"), "strings": .array([])]), .object(["sourceLanguage": .string("en"), "strings": .object(["Key": .null])]), .object([ "sourceLanguage": .string("en"), "strings": .object(["Key": .object(["localizations": .null])]), ]), ] for fixture in fixtures { #expect(throws: DevError.self) { try CheckTools.preparedCatalog(fixture) } } } private var swiftSettings: [DevJSON] { [ .object([ "tool": .string("swift"), "kind": .object(["treatAllWarnings": .object(["_0": .string("error")])]), ]), .object([ "tool": .string("swift"), "kind": .object(["enableUpcomingFeature": .object(["_0": .string("ExistentialAny")])]), ]), .object([ "tool": .string("swift"), "kind": .object(["enableUpcomingFeature": .object(["_0": .string("InternalImportsByDefault")])]), ]), ] } private func package(settings: [DevJSON], language: String = "6") -> DevJSON { .object([ "swiftLanguageVersions": .array([.string(language)]), "targets": .array([ .object(["name": .string("Fixture"), "type": .string("regular"), "settings": .array(settings)]), .object(["name": .string("Plugin"), "type": .string("plugin")]), ]), ]) } @Test func packageChecksRequireSwiftSixSettingsAndSkipPlugins() throws { #expect(try CheckTools.packageTargetCount(package(settings: swiftSettings)) == 1) #expect(throws: DevError.self) { try CheckTools.packageTargetCount(package(settings: swiftSettings, language: "5")) } for index in swiftSettings.indices { var settings = swiftSettings settings.remove(at: index) #expect(throws: DevError.self) { try CheckTools.packageTargetCount(package(settings: settings)) } } } @Test func packageChecksRequireCWarningsAsErrors() throws { let settings = DevJSON.array([ .object([ "tool": .string("c"), "kind": .object(["unsafeFlags": .object(["_0": .array([.string("-Werror")])])]), ]) ]) func fixture(settings: DevJSON) -> DevJSON { .object([ "swiftLanguageVersions": .array([.string("6")]), "targets": .array([ .object(["name": .string("CPrivateAPI"), "type": .string("regular"), "settings": settings]) ]), ]) } #expect(try CheckTools.packageTargetCount(fixture(settings: settings)) == 1) #expect(throws: DevError.self) { try CheckTools.packageTargetCount(fixture(settings: .array([]))) } #expect(throws: DevError.self) { try CheckTools.packageTargetCount(.object([:])) } } @Test func checksRejectUnknownCommandsAndMissingArguments() { #expect(throws: DevError.self) { try CheckTools.run(command: "unknown", args: []) } #expect(throws: DevError.self) { try CheckTools.run(command: "prepare-catalog", args: []) } #expect(throws: DevError.self) { try CheckTools.run(command: "check-package-settings", args: []) } } }