Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Tests/AppTests/Helpers/DependenciesProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Dependencies
import DependenciesTestSupport
import Testing


struct DependenciesProvider {
init(updateValues: @escaping @Sendable (inout DependencyValues) -> Void) {
self.updateValues = updateValues
}

var updateValues: @Sendable (inout DependencyValues) -> Void
}


extension Trait where Self == _DependenciesTrait {
static func dependencies(
_ provider: DependenciesProvider
) -> Self {
.dependencies(provider.updateValues)
}
}
19 changes: 9 additions & 10 deletions Tests/AppTests/ValidateSPIManifestControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,25 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest

@testable import App

import SPIManifest
import Testing


final class ValidateSPIManifestControllerTests: XCTestCase {
@Suite struct ValidateSPIManifestControllerTests {

func test_validationResult_basic() throws {
@Test func validationResult_basic() throws {
let yml = ValidateSPIManifest.Model.placeholderManifest

// MUT
let res = ValidateSPIManifestController.validationResult(manifest: yml)

// validate
XCTAssertEqual(res, .valid(try SPIManifest.Manifest(yml: yml)))
#expect(try res == .valid(SPIManifest.Manifest(yml: yml)))
}

func test_validationResult_decodingError() throws {
@Test func validationResult_decodingError() throws {
let yml = """
broken:
"""
Expand All @@ -40,24 +39,24 @@ final class ValidateSPIManifestControllerTests: XCTestCase {
let res = ValidateSPIManifestController.validationResult(manifest: yml)

// validate
XCTAssertEqual(res, .invalid("Decoding failed: Key not found: 'version'."))
#expect(res == .invalid("Decoding failed: Key not found: 'version'."))
}

func test_validationResult_tooLarge() throws {
@Test func validationResult_tooLarge() throws {
let targets = (0..<200).map { "Target_\($0)" }.joined(separator: ", ")
let yml = """
version: 1
builder:
configs:
- documentation_targets: [\(targets)]
"""
XCTAssert(yml.count > SPIManifest.Manifest.maxByteSize)
#expect(yml.count > SPIManifest.Manifest.maxByteSize)

// MUT
let res = ValidateSPIManifestController.validationResult(manifest: yml)

// validate
XCTAssertEqual(res, .invalid("File must not exceed \(SPIManifest.Manifest.maxByteSize) bytes. File size: \(yml.count) bytes."))
#expect(res == .invalid("File must not exceed \(SPIManifest.Manifest.maxByteSize) bytes. File size: \(yml.count) bytes."))
}

}
136 changes: 61 additions & 75 deletions Tests/AppTests/VersionDiffTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

@testable import App

import XCTest
import Testing


class VersionImmutableReferenceDiffTests: XCTestCase {
// Tests different version diffing scenarios for lower level
// [Version.ImmutableReference] interface.
// Scenarios:
// 1) branch changes commit hash
// 2) new tag is added
// 3) tag is removed
// 4) branch is removed
// 5) tag is moved
// Tests different version diffing scenarios for lower level
// [Version.ImmutableReference] interface.
// Scenarios:
// 1) branch changes commit hash
// 2) new tag is added
// 3) tag is removed
// 4) branch is removed
// 5) tag is moved
@Suite struct VersionDiffTests {

func test_diff_1() throws {
@Test func ImmutableReference_diff_1() throws {
// Branch changes commit hash
// setup
let saved: [Version.ImmutableReference] = [
Expand All @@ -42,15 +44,12 @@ class VersionImmutableReferenceDiffTests: XCTestCase {
])

// validate
XCTAssertEqual(res.toAdd,
[.init(reference: .branch("main"), commit: "hash3")])
XCTAssertEqual(res.toDelete,
[.init(reference: .branch("main"), commit: "hash1")])
XCTAssertEqual(res.toKeep,
[.init(reference: .tag(1, 2, 3), commit: "hash2")])
#expect(res.toAdd == [.init(reference: .branch("main"), commit: "hash3")])
#expect(res.toDelete == [.init(reference: .branch("main"), commit: "hash1")])
#expect(res.toKeep == [.init(reference: .tag(1, 2, 3), commit: "hash2")])
}

func test_diff_2() throws {
@Test func ImmutableReference_diff_2() throws {
// New tag is incoming
// setup
let saved: [Version.ImmutableReference] = [
Expand All @@ -66,15 +65,13 @@ class VersionImmutableReferenceDiffTests: XCTestCase {
])

// validate
XCTAssertEqual(res.toAdd,
[.init(reference: .tag(2, 0, 0), commit: "hash4")])
XCTAssertEqual(res.toDelete, [])
XCTAssertEqual(res.toKeep,
[.init(reference: .branch("main"), commit: "hash1"),
#expect(res.toAdd == [.init(reference: .tag(2, 0, 0), commit: "hash4")])
#expect(res.toDelete == [])
#expect(res.toKeep == [.init(reference: .branch("main"), commit: "hash1"),
.init(reference: .tag(1, 2, 3), commit: "hash2")])
}

func test_diff_3() throws {
@Test func ImmutableReference_diff_3() throws {
// Tag was deleted upstream
// setup
let saved: [Version.ImmutableReference] = [
Expand All @@ -88,14 +85,12 @@ class VersionImmutableReferenceDiffTests: XCTestCase {
])

// validate
XCTAssertEqual(res.toAdd, [])
XCTAssertEqual(res.toDelete,
[.init(reference: .tag(1, 2, 3), commit: "hash2")])
XCTAssertEqual(res.toKeep,
[.init(reference: .branch("main"), commit: "hash1")])
#expect(res.toAdd == [])
#expect(res.toDelete == [.init(reference: .tag(1, 2, 3), commit: "hash2")])
#expect(res.toKeep == [.init(reference: .branch("main"), commit: "hash1")])
}

func test_diff_4() throws {
@Test func ImmutableReference_diff_4() throws {
// Branch was deleted upstream
// setup
let saved: [Version.ImmutableReference] = [
Expand All @@ -109,14 +104,12 @@ class VersionImmutableReferenceDiffTests: XCTestCase {
])

// validate
XCTAssertEqual(res.toAdd, [])
XCTAssertEqual(res.toDelete,
[.init(reference: .branch("main"), commit: "hash1")])
XCTAssertEqual(res.toKeep,
[.init(reference: .tag(1, 2, 3), commit: "hash2")])
#expect(res.toAdd == [])
#expect(res.toDelete == [.init(reference: .branch("main"), commit: "hash1")])
#expect(res.toKeep == [.init(reference: .tag(1, 2, 3), commit: "hash2")])
}

func test_diff_5() throws {
@Test func ImmutableReference_diff_5() throws {
// Tag was changed - retagging a release
// setup
let saved: [Version.ImmutableReference] = [
Expand All @@ -131,47 +124,40 @@ class VersionImmutableReferenceDiffTests: XCTestCase {
])

// validate
XCTAssertEqual(res.toAdd, [.init(reference: .tag(1, 2, 3), commit: "hash3")])
XCTAssertEqual(res.toDelete, [.init(reference: .tag(1, 2, 3), commit: "hash2")])
XCTAssertEqual(res.toKeep,
[.init(reference: .branch("main"), commit: "hash1")])
#expect(res.toAdd == [.init(reference: .tag(1, 2, 3), commit: "hash3")])
#expect(res.toDelete == [.init(reference: .tag(1, 2, 3), commit: "hash2")])
#expect(res.toKeep == [.init(reference: .branch("main"), commit: "hash1")])
}

}


class VersionDiffTests: AppTestCase {
// Test [Version] based diff (higher level interface)
// Just run an integration scenario, the details are covered in the test above

func test_diff_1() async throws {
// Branch changes commit hash
// setup
let pkg = try await savePackage(on: app.db, "1")
let keptId = UUID()
let saved: [Version] = [
try .init(package: pkg, commit: "hash1", reference: .branch("main")),
try .init(id: keptId,
package: pkg, commit: "hash2", reference: .tag(1, 2, 3)),
]
try await saved.save(on: app.db)

// MUT
let res = Version.diff(local: saved, incoming: [
try .init(package: pkg, commit: "hash3", reference: .branch("main")),
try .init(package: pkg, commit: "hash2", reference: .tag(1, 2, 3)),
try .init(package: pkg, commit: "hash4", reference: .tag(2, 0, 0)),
])

// validate
XCTAssertEqual(res.toAdd.map(\.immutableReference),
[.init(reference: .branch("main"), commit: "hash3"),
.init(reference: .tag(2, 0, 0), commit: "hash4")])
XCTAssertEqual(res.toDelete.map(\.immutableReference),
[.init(reference: .branch("main"), commit: "hash1")])
XCTAssertEqual(res.toKeep.map(\.immutableReference),
[.init(reference: .tag(1, 2, 3), commit: "hash2")])
XCTAssertEqual(res.toKeep.map(\.id), [keptId])
@Test func Version_diff_1() async throws {
// Test [Version] based diff (higher level interface)
// Just run an integration scenario, the details are covered in the test above
try await withApp { app in
// Branch changes commit hash
// setup
let pkg = try await savePackage(on: app.db, "1")
let keptId = UUID()
let saved: [Version] = [
try .init(package: pkg, commit: "hash1", reference: .branch("main")),
try .init(id: keptId,
package: pkg, commit: "hash2", reference: .tag(1, 2, 3)),
]
try await saved.save(on: app.db)

// MUT
let res = Version.diff(local: saved, incoming: [
try .init(package: pkg, commit: "hash3", reference: .branch("main")),
try .init(package: pkg, commit: "hash2", reference: .tag(1, 2, 3)),
try .init(package: pkg, commit: "hash4", reference: .tag(2, 0, 0)),
])

// validate
#expect(res.toAdd.map(\.immutableReference) == [.init(reference: .branch("main"), commit: "hash3"),
.init(reference: .tag(2, 0, 0), commit: "hash4")])
#expect(res.toDelete.map(\.immutableReference) == [.init(reference: .branch("main"), commit: "hash1")])
#expect(res.toKeep.map(\.immutableReference) == [.init(reference: .tag(1, 2, 3), commit: "hash2")])
#expect(res.toKeep.map(\.id) == [keptId])
}
}

}
Loading
Loading