|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See http://swift.org/LICENSE.txt for license information |
| 8 | + See http://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | + */ |
| 10 | + |
| 11 | +import XCTest |
| 12 | +@testable import PackageRegistry |
| 13 | + |
| 14 | +private let defaultRegistryBaseURL = URL(string: "https://packages.example.com/")! |
| 15 | +private let customRegistryBaseURL = URL(string: "https://custom.packages.example.com/")! |
| 16 | + |
| 17 | +final class RegistryConfigurationTests: XCTestCase { |
| 18 | + let encoder = JSONEncoder() |
| 19 | + let decoder = JSONDecoder() |
| 20 | + |
| 21 | + func testEmptyConfiguration() throws { |
| 22 | + let configuration = RegistryConfiguration() |
| 23 | + XCTAssertNil(configuration.defaultRegistry) |
| 24 | + XCTAssertEqual(configuration.scopedRegistries, [:]) |
| 25 | + } |
| 26 | + |
| 27 | + func testRoundTripCodingForEmptyConfiguration() throws { |
| 28 | + let configuration = RegistryConfiguration() |
| 29 | + |
| 30 | + let encoded = try encoder.encode(configuration) |
| 31 | + let decoded = try decoder.decode(RegistryConfiguration.self, from: encoded) |
| 32 | + |
| 33 | + XCTAssertEqual(configuration, decoded) |
| 34 | + } |
| 35 | + |
| 36 | + func testRoundTripCodingForExampleConfiguration() throws { |
| 37 | + var configuration = RegistryConfiguration() |
| 38 | + |
| 39 | + configuration.defaultRegistry = .init(url: defaultRegistryBaseURL) |
| 40 | + configuration.scopedRegistries["foo"] = .init(url: customRegistryBaseURL) |
| 41 | + configuration.scopedRegistries["bar"] = .init(url: customRegistryBaseURL) |
| 42 | + |
| 43 | + let encoded = try encoder.encode(configuration) |
| 44 | + let decoded = try decoder.decode(RegistryConfiguration.self, from: encoded) |
| 45 | + |
| 46 | + XCTAssertEqual(configuration, decoded) |
| 47 | + } |
| 48 | + |
| 49 | + func testDecodeEmptyConfiguration() throws { |
| 50 | + let data = #""" |
| 51 | + { |
| 52 | + "registries": {}, |
| 53 | + "version": 1 |
| 54 | + } |
| 55 | + """#.data(using: .utf8)! |
| 56 | + |
| 57 | + let configuration = try decoder.decode(RegistryConfiguration.self, from: data) |
| 58 | + XCTAssertNil(configuration.defaultRegistry) |
| 59 | + XCTAssertEqual(configuration.scopedRegistries, [:]) |
| 60 | + } |
| 61 | + |
| 62 | + func testDecodeExampleConfiguration() throws { |
| 63 | + let data = #""" |
| 64 | + { |
| 65 | + "registries": { |
| 66 | + "[default]": { |
| 67 | + "url": "\#(defaultRegistryBaseURL)" |
| 68 | + }, |
| 69 | + "foo": { |
| 70 | + "url": "\#(customRegistryBaseURL)" |
| 71 | + }, |
| 72 | + "bar": { |
| 73 | + "url": "\#(customRegistryBaseURL)" |
| 74 | + }, |
| 75 | + }, |
| 76 | + "version": 1 |
| 77 | + } |
| 78 | + """#.data(using: .utf8)! |
| 79 | + |
| 80 | + let configuration = try decoder.decode(RegistryConfiguration.self, from: data) |
| 81 | + XCTAssertEqual(configuration.defaultRegistry?.url, defaultRegistryBaseURL) |
| 82 | + XCTAssertEqual(configuration.scopedRegistries["foo"]?.url, customRegistryBaseURL) |
| 83 | + XCTAssertEqual(configuration.scopedRegistries["bar"]?.url, customRegistryBaseURL) |
| 84 | + } |
| 85 | + |
| 86 | + func testDecodeConfigurationWithInvalidRegistryKey() throws { |
| 87 | + let data = #""" |
| 88 | + { |
| 89 | + "registries": { |
| 90 | + 0: "\#(customRegistryBaseURL)" |
| 91 | + }, |
| 92 | + "version": 1 |
| 93 | + } |
| 94 | + """#.data(using: .utf8)! |
| 95 | + |
| 96 | + XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: data)) |
| 97 | + } |
| 98 | + |
| 99 | + func testDecodeConfigurationWithInvalidRegistryValue() throws { |
| 100 | + let data = #""" |
| 101 | + { |
| 102 | + "registries": { |
| 103 | + "[default]": "\#(customRegistryBaseURL)" |
| 104 | + }, |
| 105 | + "version": 1 |
| 106 | + } |
| 107 | + """#.data(using: .utf8)! |
| 108 | + |
| 109 | + XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: data)) |
| 110 | + } |
| 111 | + |
| 112 | + func testDecodeConfigurationWithMissingVersion() throws { |
| 113 | + let data = #""" |
| 114 | + { |
| 115 | + "registries": {} |
| 116 | + } |
| 117 | + """#.data(using: .utf8)! |
| 118 | + |
| 119 | + XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: data)) |
| 120 | + } |
| 121 | + |
| 122 | + func testDecodeConfigurationWithInvalidVersion() throws { |
| 123 | + let data = #""" |
| 124 | + { |
| 125 | + "registries": {}, |
| 126 | + "version": 999 |
| 127 | + } |
| 128 | + """#.data(using: .utf8)! |
| 129 | + |
| 130 | + XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: data)) |
| 131 | + } |
| 132 | +} |
0 commit comments