Commit 38a4565d authored by Drew's avatar Drew

Add a diagnostic for an import directive that isn't a vector.

As part of this commit (e.g. in order to test it), we convert Package to
a throwing declaration

Fixes #11
parent 00150eb9
Pipeline #883 passed with stage
......@@ -125,6 +125,13 @@ final public class Task {
}
}
enum PackageError: ErrorType {
case NonVectorImport
case ParserFailed
case NonPackage
case NoName
}
final public class Package {
// The required properties.
public var name: String
......@@ -163,27 +170,22 @@ final public class Package {
/**Create the package.
- parameter filepath: The path to the file to load
- parameter overlay: A list of overlays to apply globally to all tasks in the package. */
public convenience init?(filepath: String, overlay: [String]) {
guard let parser = Parser(filepath: filepath) else { return nil }
public convenience init(filepath: String, overlay: [String]) throws {
//todo: why doesn't this throw?
guard let parser = Parser(filepath: filepath) else { throw PackageError.ParserFailed }
do {
let result = try parser.parse()
let basepath = filepath.toNSString.stringByDeletingLastPathComponent
self.init(type: result, overlay: overlay, pathOnDisk:basepath)
}
catch {
print("error: \(error)")
return nil
}
let result = try parser.parse()
let basepath = filepath.toNSString.stringByDeletingLastPathComponent
try self.init(type: result, overlay: overlay, pathOnDisk:basepath)
}
public init?(type: ParseType, overlay requestedGlobalOverlays: [String], pathOnDisk: String) {
if type.name != "package" { return nil }
public init(type: ParseType, overlay requestedGlobalOverlays: [String], pathOnDisk: String) throws {
if type.name != "package" { throw PackageError.NonPackage }
if let value = type.properties["name"]?.string { self.name = value }
else {
print("ERROR: No name specified for the package.")
return nil
throw PackageError.NoName
}
if let value = type.properties["version"]?.string { self.version = value }
......@@ -198,14 +200,15 @@ final public class Package {
var remotePackages: [Package] = []
//load remote packages
if let imports = type.properties["import"]?.vector {
if let imports_nv = type.properties["import"] {
guard let imports = imports_nv.vector else {
throw PackageError.NonVectorImport
}
for importFile in imports {
guard let importFileString = importFile.string else { fatalError("Non-string import \(importFile)")}
let adjustedImportPath = (pathOnDisk.pathWithTrailingSlash + importFileString).toNSString.stringByDeletingLastPathComponent.pathWithTrailingSlash
let adjustedFileName = importFileString.toNSString.lastPathComponent
guard let remotePackage = Package(filepath: adjustedImportPath + adjustedFileName, overlay: requestedGlobalOverlays) else {
fatalError("Can't load remote package \(adjustedImportPath + adjustedFileName)")
}
let remotePackage = try Package(filepath: adjustedImportPath + adjustedFileName, overlay: requestedGlobalOverlays)
remotePackage.adjustedImportPath = adjustedImportPath
remotePackages.append(remotePackage)
}
......
(package
:name "non-vector-import"
:import "Not a vector"
)
; End of the sample.
\ No newline at end of file
......@@ -22,7 +22,8 @@ class PackageTests: Test {
PackageTests.testBasic,
PackageTests.testImport,
PackageTests.testOverlays,
PackageTests.testExportedOverlays
PackageTests.testExportedOverlays,
PackageTests.nonVectorImport
]
let filename = __FILE__
......@@ -35,7 +36,7 @@ class PackageTests: Test {
}
let result = try parser.parse()
guard let package = Package(type: result, overlay: [], pathOnDisk: "./tests/collateral") else { try test.assert(false); return }
let package = try Package(type: result, overlay: [], pathOnDisk: "./tests/collateral")
try test.assert(package.name == "basic")
try test.assert(package.version == "0.1.0-dev")
......@@ -54,7 +55,7 @@ class PackageTests: Test {
static func testImport() throws {
let filepath = "./tests/collateral/import_src.atpkg"
guard let package = Package(filepath: filepath, overlay: []) else { print("error"); try test.assert(false); return }
let package = try Package(filepath: filepath, overlay: [])
try test.assert(package.tasks["import_dst.build"] != nil)
try test.assert(package.tasks["import_dst.build"]!.importedPath == "./tests/collateral/")
......@@ -62,7 +63,7 @@ class PackageTests: Test {
static func testOverlays() throws {
let filepath = "./tests/collateral/overlays.atpkg"
guard let package = Package(filepath: filepath, overlay: []) else { print("error"); try test.assert(false); return }
let package = try Package(filepath: filepath, overlay: [])
guard let compileOptions = package.tasks["build"]?["compileOptions"]?.vector else {
fatalError("No compile options?")
}
......@@ -70,7 +71,7 @@ class PackageTests: Test {
try test.assert(compileOptions[0].string == "-D")
try test.assert(compileOptions[1].string == "AWESOME")
guard let package2 = Package(filepath: filepath, overlay: ["more-awesome"]) else { print("error"); try test.assert(false); return }
let package2 = try Package(filepath: filepath, overlay: ["more-awesome"])
guard let compileOptions2 = package2.tasks["build"]?["compileOptions"]?.vector else {
fatalError("no compile options?")
}
......@@ -80,7 +81,7 @@ class PackageTests: Test {
try test.assert(compileOptions2[2].string == "-D")
try test.assert(compileOptions2[3].string == "MORE_AWESOME")
guard let package3 = Package(filepath: filepath, overlay: ["most-taskspecific"]) else { print("error"); try test.assert(false); return }
let package3 = try Package(filepath: filepath, overlay: ["most-taskspecific"])
guard let compileOptions3 = package3.tasks["build"]?["compileOptions"]?.vector else {
fatalError("no compile options?")
}
......@@ -90,7 +91,7 @@ class PackageTests: Test {
try test.assert(compileOptions3[2].string == "-D")
try test.assert(compileOptions3[3].string == "MOST_AWESOME")
guard let package4 = Package(filepath: filepath, overlay: ["most-taskspecific-two"]) else { print("error"); try test.assert(false); return }
let package4 = try Package(filepath: filepath, overlay: ["most-taskspecific-two"])
guard let compileOptions4 = package4.tasks["build"]?["compileOptions"]?.vector else {
fatalError("no compile options?")
}
......@@ -100,13 +101,13 @@ class PackageTests: Test {
try test.assert(compileOptions4[2].string == "-D")
try test.assert(compileOptions4[3].string == "MOST_AWESOME")
guard let package5 = Package(filepath: filepath, overlay: ["stringOption"]) else { print("error"); try test.assert(false); return }
let package5 = try Package(filepath: filepath, overlay: ["stringOption"])
guard let stringOption = package5.tasks["build"]?["stringOption"]?.string else {
fatalError("no string option?")
}
try test.assert(stringOption == "stringOption")
guard let package6 = Package(filepath: filepath, overlay: ["emptyVecOption"]) else { print("error"); try test.assert(false); return }
let package6 = try Package(filepath: filepath, overlay: ["emptyVecOption"])
guard let vecOption = package6.tasks["build"]?["emptyVecOption"]?.vector else {
fatalError("no vec option?")
}
......@@ -114,7 +115,7 @@ class PackageTests: Test {
try test.assert(vecOption[0].string == "OVERLAY")
guard let package7 = Package(filepath: filepath, overlay: ["boolOption"]) else { print("error"); try test.assert(false); return }
let package7 = try Package(filepath: filepath, overlay: ["boolOption"])
guard let boolOption = package7.tasks["build"]?["boolOption"]?.bool else {
fatalError("no bool option?")
}
......@@ -124,7 +125,7 @@ class PackageTests: Test {
static func testExportedOverlays() throws {
let filepath = "./tests/collateral/overlays_src.atpkg"
guard let package2 = Package(filepath: filepath, overlay: []) else { print("error"); try test.assert(false); return }
let package2 = try Package(filepath: filepath, overlay: [])
guard let compileOptions2 = package2.tasks["build"]?["compileOptions"]?.vector else {
fatalError("no compile options?")
}
......@@ -137,4 +138,11 @@ class PackageTests: Test {
try test.assert(compileOptions2[5].string == "MOST_AWESOME")
}
static func nonVectorImport() throws {
let filepath = "./tests/collateral/non_vector_import.atpkg"
if let _ = try? Package(filepath: filepath, overlay: []) {
try test.assert(false) //no diagnostic
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment