PackageTests.swift 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright (c) 2016 Anarchy Tools 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 Foundation
import atpkg

class PackageTests: Test {
    required init() {}
    let tests = [
22
        PackageTests.testBasic,
Drew's avatar
Drew committed
23
        PackageTests.testImport,
Drew's avatar
Drew committed
24
        PackageTests.testOverlays,
Drew's avatar
Drew committed
25
        PackageTests.testExportedOverlays,
26
        PackageTests.testChainedImports,
Drew's avatar
Drew committed
27
        PackageTests.testImportPaths,
28
        PackageTests.testChainedImportOverlays,
29
        PackageTests.nonVectorImport,
Drew's avatar
Drew committed
30
        PackageTests.testRequireOverlays
31

32 33
    ]

Drew's avatar
Drew committed
34
    let filename = #file
35 36
    
    static func testBasic() throws {
Drew's avatar
Drew committed
37
        let filepath = "./tests/collateral/basic.atpkg"
38 39 40 41 42 43

        guard let parser = Parser(filepath: filepath) else {
            try test.assert(false); return
        }
        
        let result = try parser.parse()
Drew's avatar
Drew committed
44
        let package = try Package(type: result, overlay: [], pathOnDisk: "./tests/collateral", focusOnTask: nil)
45 46 47
        
        try test.assert(package.name == "basic")
        try test.assert(package.version == "0.1.0-dev")
Drew's avatar
Drew committed
48
        try test.assert(package.tasks.count == 2) //indexed twice, by qualified and unqualified name
49
        for (key, task) in package.tasks {
Drew's avatar
Drew committed
50
            try test.assert(key == "build" || key == "basic.build")
51
            try test.assert(task.tool == "lldb-build")
52 53
            try test.assert(task["name"]?.string == "json-swift")
            try test.assert(task["output-type"]?.string == "lib")
54 55 56
            try test.assert(task["sources"]?.vector?.count == 2)
            try test.assert(task["sources"]?.vector?[0].string == "src/**.swift")
            try test.assert(task["sources"]?.vector?[1].string == "lib/**.swift")
57 58
        }
    }
59 60 61

    static func testImport() throws {
        let filepath = "./tests/collateral/import_src.atpkg"
Drew's avatar
Drew committed
62
        let package = try Package(filepath: filepath, overlay: [], focusOnTask: nil)
63 64

        try test.assert(package.tasks["import_dst.build"] != nil)
Drew's avatar
Drew committed
65
        try test.assert(package.tasks["import_dst.build"]!.importedPath == "./tests/collateral/")
66
    }
Drew's avatar
Drew committed
67

Drew's avatar
Drew committed
68 69
    static func testOverlays() throws {
        let filepath = "./tests/collateral/overlays.atpkg"
Drew's avatar
Drew committed
70
        let package = try Package(filepath: filepath, overlay: [], focusOnTask: nil)
71
        guard let compileOptions = package.tasks["build"]?["compile-options"]?.vector else {
Drew's avatar
Drew committed
72 73
            fatalError("No compile options?")
        }
74
        try test.assert(compileOptions.count == 2)
Drew's avatar
Drew committed
75 76 77
        try test.assert(compileOptions[0].string == "-D")
        try test.assert(compileOptions[1].string == "AWESOME")

Drew's avatar
Drew committed
78
        let package2 = try Package(filepath: filepath, overlay: ["more-awesome"], focusOnTask:nil)
79
        guard let compileOptions2 = package2.tasks["build"]?["compile-options"]?.vector else {
Drew's avatar
Drew committed
80 81
            fatalError("no compile options?")
        }
82
        try test.assert(compileOptions2.count == 4)
Drew's avatar
Drew committed
83 84 85 86
        try test.assert(compileOptions2[0].string == "-D")
        try test.assert(compileOptions2[1].string == "AWESOME")
        try test.assert(compileOptions2[2].string == "-D")
        try test.assert(compileOptions2[3].string == "MORE_AWESOME")
87

Drew's avatar
Drew committed
88
        let package3 = try Package(filepath: filepath, overlay: ["most-taskspecific"], focusOnTask: nil)
89
        guard let compileOptions3 = package3.tasks["build"]?["compile-options"]?.vector else {
90 91 92 93 94 95 96 97
            fatalError("no compile options?")
        }
        try test.assert(compileOptions3.count == 4)
        try test.assert(compileOptions3[0].string == "-D")
        try test.assert(compileOptions3[1].string == "AWESOME")
        try test.assert(compileOptions3[2].string == "-D")
        try test.assert(compileOptions3[3].string == "MOST_AWESOME")

Drew's avatar
Drew committed
98
        let package4 = try Package(filepath: filepath, overlay: ["most-taskspecific-two"], focusOnTask: nil)
99
        guard let compileOptions4 = package4.tasks["build"]?["compile-options"]?.vector else {
100 101 102 103 104 105 106
            fatalError("no compile options?")
        }
        try test.assert(compileOptions4.count == 4)
        try test.assert(compileOptions4[0].string == "-D")
        try test.assert(compileOptions4[1].string == "AWESOME")
        try test.assert(compileOptions4[2].string == "-D")
        try test.assert(compileOptions4[3].string == "MOST_AWESOME")
Drew's avatar
Drew committed
107

Drew's avatar
Drew committed
108
        let package5 = try Package(filepath: filepath, overlay: ["string-option"], focusOnTask: nil)
109
        guard let stringOption = package5.tasks["build"]?["string-option"]?.string else {
Drew's avatar
Drew committed
110 111
            fatalError("no string option?")
        }
112 113
        try test.assert(stringOption == "stringOption")

Drew's avatar
Drew committed
114
        let package6 = try Package(filepath: filepath, overlay: ["empty-vec-option"], focusOnTask: nil)
115
        guard let vecOption = package6.tasks["build"]?["empty-vec-option"]?.vector else {
116 117 118 119 120
            fatalError("no vec option?")
        }
        try test.assert(vecOption.count == 1)

        try test.assert(vecOption[0].string == "OVERLAY")
Drew's avatar
Drew committed
121

Drew's avatar
Drew committed
122
        let package7 = try Package(filepath: filepath, overlay: ["bool-option"], focusOnTask: nil)
123
        guard let boolOption = package7.tasks["build"]?["bool-option"]?.bool else {
Drew's avatar
Drew committed
124 125 126
            fatalError("no bool option?")
        }
        try test.assert(boolOption == true)
Drew's avatar
Drew committed
127
    }
Drew's avatar
Drew committed
128

Drew's avatar
Drew committed
129 130
    static func testExportedOverlays() throws {
        let filepath = "./tests/collateral/overlays_src.atpkg"
Drew's avatar
Drew committed
131

Drew's avatar
Drew committed
132
        let package2 = try Package(filepath: filepath, overlay: [], focusOnTask: nil)
133
        guard let compileOptions2 = package2.tasks["build"]?["compile-options"]?.vector else {
Drew's avatar
Drew committed
134 135 136 137 138 139 140 141 142 143 144
            fatalError("no compile options?")
        }
        try test.assert(compileOptions2.count == 6)
        try test.assert(compileOptions2[0].string == "-D")
        try test.assert(compileOptions2[1].string == "AWESOME")
        try test.assert(compileOptions2[2].string == "-D")
        try test.assert(compileOptions2[3].string == "MORE_AWESOME")
        try test.assert(compileOptions2[4].string == "-D")
        try test.assert(compileOptions2[5].string == "MOST_AWESOME")

    }
Drew's avatar
Drew committed
145

Drew's avatar
Drew committed
146 147
    static func testChainedImports () throws {
        let filepath = "./tests/collateral/chained_imports/a.atpkg"
Drew's avatar
Drew committed
148
        let package = try Package(filepath: filepath, overlay: [], focusOnTask: nil)
Drew's avatar
Drew committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        guard let a_default_unqualified = package.tasks["default"] else {
            fatalError("No default task")
        }
        try test.assert(a_default_unqualified["name"]?.string == "a_default")

        guard let a_default_qualified = package.tasks["a.default"] else {
            fatalError("No default task (qualified)")
        }
        try test.assert(a_default_qualified["name"]?.string == "a_default")

        guard let b_default_qualified = package.tasks["b.default"] else {
            fatalError("No default task in b")
        }
        try test.assert(b_default_qualified["name"]?.string == "b_default")

        guard let c_default_qualified = package.tasks["c.default"] else {
            fatalError("No default task in c")
        }
        try test.assert(c_default_qualified["name"]?.string == "c_default")
168 169

        //check package dependency graph
Drew's avatar
Drew committed
170
        let _ = package.prunedDependencyGraph(task: a_default_unqualified)
171
        
Drew's avatar
Drew committed
172
    }
173 174 175

    static func testImportPaths () throws {
        let filepath = "./tests/collateral/import_paths/a.atpkg"
Drew's avatar
Drew committed
176
        let package = try Package(filepath: filepath, overlay: [], focusOnTask: nil)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        guard let a_default_unqualified = package.tasks["default"] else {
            fatalError("No default task")
        }
        try test.assert(a_default_unqualified["name"]?.string == "a_default")

        guard let a_default_qualified = package.tasks["a.default"] else {
            fatalError("No default task (qualified)")
        }
        try test.assert(a_default_qualified["name"]?.string == "a_default")

        guard let b_default_qualified = package.tasks["b.default"] else {
            fatalError("No default task in b")
        }
        try test.assert(b_default_qualified["name"]?.string == "b_default")

        guard let c_default_qualified = package.tasks["c.default"] else {
            fatalError("No default task in c")
        }
        try test.assert(c_default_qualified["name"]?.string == "c_default")

        //check package dependency graph
Drew's avatar
Drew committed
198
        let _ = package.prunedDependencyGraph(task: a_default_unqualified)
199 200 201 202 203 204

        //check each import path
        try test.assert(a_default_unqualified.importedPath == "./tests/collateral/import_paths/")
        try test.assert(a_default_qualified.importedPath == "./tests/collateral/import_paths/")
        try test.assert(b_default_qualified.importedPath == "./tests/collateral/import_paths/b/")
        try test.assert(c_default_qualified.importedPath == "./tests/collateral/import_paths/b/c/")
Drew's avatar
Drew committed
205
    }
206

Drew's avatar
Drew committed
207 208
    static func testChainedImportOverlays() throws {
        let filepath = "./tests/collateral/chained_import_overlays/a.atpkg"
Drew's avatar
Drew committed
209
        let package = try Package(filepath: filepath, overlay: ["b.foo"], focusOnTask: nil)
Drew's avatar
Drew committed
210
        guard let a_qualified = package.tasks["a.default"] else { print("error"); try test.assert(false); return }
211
        guard let options = a_qualified["compile-options"]?.vector else {
Drew's avatar
Drew committed
212 213 214 215 216 217 218
            fatalError("Invalid options vector")
        }
        try test.assert(options.count == 1)
        for opt in options {
            guard let str = opt.string else { fatalError("Non-string opt \(opt)")}
            try test.assert(str == "foo")
        }
219
    }
220

Drew's avatar
Drew committed
221 222
    static func testRequireOverlays() throws {
        let filepath = "./tests/collateral/require_overlays.atpkg"
223
        do {
Drew's avatar
Drew committed
224
            let _ = try Package(filepath: filepath, overlay: [], focusOnTask: nil)
Drew's avatar
Drew committed
225 226 227
            print("Overlays were not required")
            try test.assert(false)
        }
228 229 230
        catch {}

        do {
Drew's avatar
Drew committed
231
            let _ = try Package(filepath: filepath, overlay: ["osx"], focusOnTask: nil) 
232 233
        }
        catch {
Drew's avatar
Drew committed
234 235
            print("Overlays were provided")
            try test.assert(false)
236 237 238 239

        }
    }

240 241
    static func nonVectorImport() throws {
        let filepath = "./tests/collateral/non_vector_import.atpkg"
Drew's avatar
Drew committed
242
        if let _ = try? Package(filepath: filepath, overlay: [], focusOnTask: nil) {
243
            try test.assert(false) //no diagnostic
Drew's avatar
Drew committed
244 245
        }
    }
246
}