-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from cybozu/swift-testing
Migrate to Swift Testing from XCTest
- Loading branch information
Showing
4 changed files
with
187 additions
and
350 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
Tests/LicenseCheckerModuleTests/LicenseType+Extension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import LicenseCheckerModule | ||
|
||
extension LicenseType: @unchecked Sendable { | ||
var containerDirectoryName: String { | ||
switch self { | ||
case .apache: | ||
"SourcePackagesApache" | ||
case .mit: | ||
"SourcePackagesMIT" | ||
case .bsd: | ||
"SourcePackagesBSD" | ||
case .zlib: | ||
"SourcePackagesZlib" | ||
case .boringSSL: | ||
"SourcePackagesBoringSSL" | ||
case .unknown: | ||
"" | ||
} | ||
} | ||
|
||
var packageDirectoryName: String { | ||
switch self { | ||
case .apache: | ||
"apache-package" | ||
case .mit: | ||
"mit-package" | ||
case .bsd: | ||
"bsd-package" | ||
case .zlib: | ||
"zlib-package" | ||
case .boringSSL: | ||
"boringssl-package" | ||
case .unknown: | ||
"" | ||
} | ||
} | ||
} |
241 changes: 56 additions & 185 deletions
241
Tests/LicenseCheckerModuleTests/PackageParserTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,208 +1,79 @@ | ||
import XCTest | ||
import Testing | ||
import TestResources | ||
@testable import LicenseCheckerModule | ||
|
||
final class PackageParserTests: XCTestCase { | ||
let testResources = TestResources() | ||
let whiteListDummy = WhiteList(licenses: nil, libraries: nil) | ||
struct PackageParserTests { | ||
private let testResources = TestResources() | ||
|
||
func test_init_PackageParser_success() throws { | ||
@Test("If workspace-state is normal, PackageParser initialization succeeds.") | ||
func init_PackageParser_success() async throws { | ||
let jsonPath = "SourcePackagesUnknown/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
|
||
let sut = PackageParser(url: jsonURL) | ||
XCTAssertNotNil(sut) | ||
let jsonURL = try #require(testResources.getJsonUrl(jsonPath)) | ||
let actual = PackageParser(url: jsonURL) | ||
#expect(actual != nil) | ||
} | ||
|
||
func test_init_PackageParser_failure() throws { | ||
|
||
@Test("If workspace-state is broken, PackageParser initialization fails.") | ||
func init_PackageParser_failure() async throws { | ||
let jsonPath = "SourcePackagesWorkspaceStateBroken/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
|
||
let sut = PackageParser(url: jsonURL) | ||
XCTAssertNil(sut) | ||
} | ||
|
||
// MARK: Apache | ||
func test_extractLicense_apache() throws { | ||
let jsonPath = "SourcePackagesUnknown/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
let resourceURL = try XCTUnwrap(testResources.resourceURL) | ||
let directoryURL = resourceURL.appendingPathComponent("SourcePackagesApache") | ||
.appendingPathComponent("checkouts") | ||
.appendingPathComponent("apache-package") | ||
let sut = packageParser.extractLicense(directoryURL: directoryURL) | ||
XCTAssertEqual(sut, LicenseType.apache) | ||
let jsonURL = try #require(testResources.getJsonUrl(jsonPath)) | ||
|
||
let actual = PackageParser(url: jsonURL) | ||
#expect(actual == nil) | ||
} | ||
|
||
func test_extractLicence_apache() throws { | ||
@Test( | ||
"The license file is successfully extracted.", | ||
arguments: [LicenseType.apache, .mit, .bsd, .zlib, .boringSSL] | ||
) | ||
func extract_license(_ licenseType: LicenseType) async throws { | ||
let jsonPath = "SourcePackagesUnknown/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
let resourceURL = try XCTUnwrap(testResources.resourceURL) | ||
let directoryURL = resourceURL.appendingPathComponent("SourcePackagesLICENCE") | ||
let jsonURL = try #require(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try #require(PackageParser(url: jsonURL)) | ||
let resourceURL = try #require(testResources.resourceURL) | ||
let directoryURL = resourceURL.appendingPathComponent(licenseType.containerDirectoryName) | ||
.appendingPathComponent("checkouts") | ||
.appendingPathComponent("apache-package") | ||
let sut = packageParser.extractLicense(directoryURL: directoryURL) | ||
XCTAssertEqual(sut, LicenseType.apache) | ||
} | ||
|
||
func test_parse_apache() throws { | ||
let jsonPath = "SourcePackagesApache/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
|
||
let resourcePath = try XCTUnwrap(testResources.resourcePath) | ||
let checkoutsPath = "\(resourcePath)/SourcePackagesApache/checkouts" | ||
let sut = packageParser.parse(with: checkoutsPath, whiteList: whiteListDummy) | ||
|
||
let expect = Acknowledgement( | ||
libraryName: "apache-package", | ||
licenseType: .apache, | ||
isForbidden: true | ||
) | ||
XCTAssertEqual(sut, [expect]) | ||
} | ||
|
||
// MARK: MIT | ||
func test_extractLicense_mit() throws { | ||
let jsonPath = "SourcePackagesUnknown/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
|
||
let resourceURL = try XCTUnwrap(testResources.resourceURL) | ||
let directoryURL = resourceURL.appendingPathComponent("SourcePackagesMIT") | ||
.appendingPathComponent("checkouts") | ||
.appendingPathComponent("mit-package") | ||
|
||
let sut = packageParser.extractLicense(directoryURL: directoryURL) | ||
XCTAssertEqual(sut, LicenseType.mit) | ||
.appendingPathComponent(licenseType.packageDirectoryName) | ||
let actual = packageParser.extractLicense(directoryURL: directoryURL) | ||
#expect(actual == licenseType) | ||
} | ||
|
||
func test_extractLicence_mit() throws { | ||
@Test( | ||
"The licence file is successfully extracted.", | ||
arguments: [LicenseType.apache, .mit] | ||
) | ||
func extract_licence(_ licenseType: LicenseType) async throws { | ||
let jsonPath = "SourcePackagesUnknown/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
|
||
let resourceURL = try XCTUnwrap(testResources.resourceURL) | ||
let jsonURL = try #require(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try #require(PackageParser(url: jsonURL)) | ||
let resourceURL = try #require(testResources.resourceURL) | ||
let directoryURL = resourceURL.appendingPathComponent("SourcePackagesLICENCE") | ||
.appendingPathComponent("checkouts") | ||
.appendingPathComponent("mit-package") | ||
|
||
let sut = packageParser.extractLicense(directoryURL: directoryURL) | ||
XCTAssertEqual(sut, LicenseType.mit) | ||
} | ||
|
||
func test_parse_mit() throws { | ||
let jsonPath = "SourcePackagesMIT/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
|
||
let resourcePath = try XCTUnwrap(testResources.resourcePath) | ||
let checkoutsPath = "\(resourcePath)/SourcePackagesMIT/checkouts" | ||
let sut = packageParser.parse(with: checkoutsPath, whiteList: whiteListDummy) | ||
|
||
let expect = Acknowledgement( | ||
libraryName: "mit-package", | ||
licenseType: .mit, | ||
isForbidden: true | ||
) | ||
XCTAssertEqual(sut, [expect]) | ||
} | ||
|
||
// MARK: BSD | ||
func test_extractLicense_bsd() throws { | ||
let jsonPath = "SourcePackagesUnknown/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
|
||
let resourceURL = try XCTUnwrap(testResources.resourceURL) | ||
let directoryURL = resourceURL.appendingPathComponent("SourcePackagesBSD") | ||
.appendingPathComponent("checkouts") | ||
.appendingPathComponent("bsd-package") | ||
|
||
let sut = packageParser.extractLicense(directoryURL: directoryURL) | ||
XCTAssertEqual(sut, LicenseType.bsd) | ||
} | ||
|
||
func test_parse_bsd() throws { | ||
let jsonPath = "SourcePackagesBSD/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
|
||
let resourcePath = try XCTUnwrap(testResources.resourcePath) | ||
let checkoutsPath = "\(resourcePath)/SourcePackagesBSD/checkouts" | ||
let sut = packageParser.parse(with: checkoutsPath, whiteList: whiteListDummy) | ||
|
||
let expect = Acknowledgement( | ||
libraryName: "bsd-package", | ||
licenseType: .bsd, | ||
isForbidden: true | ||
) | ||
XCTAssertEqual(sut, [expect]) | ||
} | ||
|
||
// MARK: zlib | ||
func test_extractLicense_zlib() throws { | ||
let jsonPath = "SourcePackagesUnknown/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
|
||
let resourceURL = try XCTUnwrap(testResources.resourceURL) | ||
let directoryURL = resourceURL.appendingPathComponent("SourcePackagesZlib") | ||
.appendingPathComponent("checkouts") | ||
.appendingPathComponent("zlib-package") | ||
|
||
let sut = packageParser.extractLicense(directoryURL: directoryURL) | ||
XCTAssertEqual(sut, LicenseType.zlib) | ||
.appendingPathComponent(licenseType.packageDirectoryName) | ||
let actual = packageParser.extractLicense(directoryURL: directoryURL) | ||
#expect(actual == licenseType) | ||
} | ||
|
||
func test_parse_zlib() throws { | ||
let jsonPath = "SourcePackagesZlib/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
|
||
let resourcePath = try XCTUnwrap(testResources.resourcePath) | ||
let checkoutsPath = "\(resourcePath)/SourcePackagesZlib/checkouts" | ||
let sut = packageParser.parse(with: checkoutsPath, whiteList: whiteListDummy) | ||
|
||
let expect = Acknowledgement( | ||
libraryName: "zlib-package", | ||
licenseType: .zlib, | ||
isForbidden: true | ||
) | ||
XCTAssertEqual(sut, [expect]) | ||
} | ||
|
||
// MARK: BoringSSL | ||
func test_extractLicense_boringssl() throws { | ||
let jsonPath = "SourcePackagesUnknown/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
|
||
let resourceURL = try XCTUnwrap(testResources.resourceURL) | ||
let directoryURL = resourceURL.appendingPathComponent("SourcePackagesBoringSSL") | ||
.appendingPathComponent("checkouts") | ||
.appendingPathComponent("boringssl-package") | ||
|
||
let sut = packageParser.extractLicense(directoryURL: directoryURL) | ||
XCTAssertEqual(sut, LicenseType.boringSSL) | ||
} | ||
|
||
func test_parse_boringssl() throws { | ||
let jsonPath = "SourcePackagesBoringSSL/workspace-state" | ||
let jsonURL = try XCTUnwrap(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try XCTUnwrap(PackageParser(url: jsonURL)) | ||
|
||
let resourcePath = try XCTUnwrap(testResources.resourcePath) | ||
let checkoutsPath = "\(resourcePath)/SourcePackagesBoringSSL/checkouts" | ||
let sut = packageParser.parse(with: checkoutsPath, whiteList: whiteListDummy) | ||
|
||
@Test( | ||
"The license file is successfully parsed.", | ||
arguments: [LicenseType.apache, .mit, .bsd, .zlib, .boringSSL] | ||
) | ||
func parse_license(_ licenseType: LicenseType) async throws { | ||
let jsonPath = "\(licenseType.containerDirectoryName)/workspace-state" | ||
let jsonURL = try #require(testResources.getJsonUrl(jsonPath)) | ||
let packageParser = try #require(PackageParser(url: jsonURL)) | ||
|
||
let resourcePath = try #require(testResources.resourcePath) | ||
let checkoutsPath = "\(resourcePath)/\(licenseType.containerDirectoryName)/checkouts" | ||
let whiteListDummy = WhiteList(licenses: nil, libraries: nil) | ||
let actual = packageParser.parse(with: checkoutsPath, whiteList: whiteListDummy) | ||
|
||
let expect = Acknowledgement( | ||
libraryName: "boringssl-package", | ||
licenseType: .boringSSL, | ||
libraryName: licenseType.packageDirectoryName, | ||
licenseType: licenseType, | ||
isForbidden: true | ||
) | ||
XCTAssertEqual(sut, [expect]) | ||
#expect(actual == [expect]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,42 @@ | ||
import XCTest | ||
import TestResources | ||
import Foundation | ||
import Testing | ||
@testable import LicenseCheckerModule | ||
|
||
final class WhiteListTests: XCTestCase { | ||
|
||
func getJsonUrl(_ jsonName: String) -> URL? { | ||
return Bundle.module.url(forResource: jsonName, withExtension: "json") | ||
struct WhiteListTests { | ||
private func getJsonUrl(_ jsonName: String) -> URL? { | ||
Bundle.module.url(forResource: jsonName, withExtension: "json") | ||
} | ||
|
||
func test_load_broken_white_list() throws { | ||
let jsonURL = try XCTUnwrap(getJsonUrl("white-list-broken")) | ||
|
||
let sut = WhiteList.load(url: jsonURL) | ||
XCTAssertNil(sut) | ||
@Test("If the whitelist is broken, loading fails.") | ||
func load_broken_white_list_failure() throws { | ||
let jsonURL = try #require(getJsonUrl("white-list-broken")) | ||
let actual = WhiteList.load(url: jsonURL) | ||
#expect(actual == nil) | ||
} | ||
|
||
|
||
func test_load_empty_white_list() throws { | ||
let jsonURL = try XCTUnwrap(getJsonUrl("white-list-empty")) | ||
|
||
let sut = WhiteList.load(url: jsonURL) | ||
XCTAssertNotNil(sut) | ||
|
||
let actual = try XCTUnwrap(sut) | ||
XCTAssertNotNil(actual.licenses) | ||
XCTAssertEqual(actual.licenses?.isEmpty, true) | ||
XCTAssertNotNil(actual.libraries) | ||
XCTAssertEqual(actual.libraries?.isEmpty, true) | ||
|
||
@Test("If the whitelist is empty, loading succeeds.") | ||
func load_empty_white_list_success() throws { | ||
let jsonURL = try #require(getJsonUrl("white-list-empty")) | ||
let actual = try #require(WhiteList.load(url: jsonURL)) | ||
#expect(actual.licenses != nil) | ||
#expect(actual.licenses?.isEmpty == true) | ||
#expect(actual.libraries != nil) | ||
#expect(actual.libraries?.isEmpty == true) | ||
} | ||
|
||
func test_load_licenses() throws { | ||
let jsonURL = try XCTUnwrap(getJsonUrl("white-list-one-license")) | ||
|
||
let sut = WhiteList.load(url: jsonURL) | ||
XCTAssertNotNil(sut) | ||
|
||
let actual = try XCTUnwrap(sut) | ||
XCTAssertEqual(actual.licenses, ["Apache"]) | ||
XCTAssertNil(actual.libraries) | ||
|
||
@Test("If the whitelist is normal and contains a license, loading it succeeds.") | ||
func load_normal_white_list_with_license_success() throws { | ||
let jsonURL = try #require(getJsonUrl("white-list-one-license")) | ||
let actual = try #require(WhiteList.load(url: jsonURL)) | ||
#expect(actual.licenses == ["Apache"]) | ||
#expect(actual.libraries == nil) | ||
} | ||
|
||
func test_load_libraries() throws { | ||
let jsonURL = try XCTUnwrap(getJsonUrl("white-list-one-library")) | ||
|
||
let sut = WhiteList.load(url: jsonURL) | ||
XCTAssertNotNil(sut) | ||
|
||
let actual = try XCTUnwrap(sut) | ||
XCTAssertNil(actual.licenses) | ||
XCTAssertEqual(actual.libraries, ["some-package"]) | ||
|
||
@Test("If the whitelist is normal and contains a library, loading it succeeds.") | ||
func load_normal_white_list_with_library_success() throws { | ||
let jsonURL = try #require(getJsonUrl("white-list-one-library")) | ||
let actual = try #require(WhiteList.load(url: jsonURL)) | ||
#expect(actual.licenses == nil) | ||
#expect(actual.libraries == ["some-package"]) | ||
} | ||
} |
Oops, something went wrong.