Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate XCTest with Spectre for nicer tests in Xcode #316

Merged
merged 1 commit into from
Jun 8, 2018
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
10 changes: 5 additions & 5 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"repositoryURL": "https://github.com/onevcat/Rainbow.git",
"state": {
"branch": null,
"revision": "f69961599ad524251d677fbec9e4bac57385d6fc",
"version": "3.1.1"
"revision": "797a68d0a642609424b08f11eb56974a54d5f6e2",
"version": "3.1.4"
}
},
{
Expand All @@ -57,11 +57,11 @@
},
{
"package": "Spectre",
"repositoryURL": "https://github.com/kylef/Spectre.git",
"repositoryURL": "https://github.com/yonaskolb/Spectre.git",
"state": {
"branch": null,
"revision": "e34d5687e1e9d865e3527dd58bc2f7464ef6d936",
"version": "0.8.0"
"revision": "2b520ec417fc515c70e7093ae3ee321c3237e215",
"version": "0.8.1"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
.package(url: "https://github.com/kylef/Commander.git", from: "0.8.0"),
.package(url: "https://github.com/jpsim/Yams.git", from: "0.3.6"),
.package(url: "https://github.com/yonaskolb/JSONUtilities.git", from: "3.3.0"),
.package(url: "https://github.com/kylef/Spectre.git", from: "0.8.0"),
.package(url: "https://github.com/yonaskolb/Spectre.git", from: "0.8.1"),
.package(url: "https://github.com/onevcat/Rainbow.git", from: "3.0.0"),
.package(url: "https://github.com/xcodeswift/xcproj.git", from: "4.1.0")
],
Expand Down
9 changes: 0 additions & 9 deletions Tests/LinuxMain.swift

This file was deleted.

81 changes: 0 additions & 81 deletions Tests/XcodeGenKitTests/FixtureTests.swift

This file was deleted.

47 changes: 47 additions & 0 deletions Tests/XcodeGenKitTests/GeneratorHelpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import PathKit
import ProjectSpec
import Spectre
import XcodeGenKit
import xcproj
import XCTest
import Yams

extension Project {

func generateXcodeProject(file: String = #file, line: Int = #line) throws -> XcodeProj {
return try doThrowing(file: file, line: line) {
let generator = ProjectGenerator(project: self)
return try generator.generateXcodeProject()
}
}

func generatePbxProj(file: String = #file, line: Int = #line) throws -> PBXProj {
return try doThrowing(file: file, line: line) {
let xcodeProject = try generateXcodeProject().pbxproj
try xcodeProject.validate()
return xcodeProject
}
}
}

extension PBXProj {

// validates that a PBXProj is correct
// TODO: Use xclint?
func validate() throws {
let mainGroup = try getMainGroup()

func validateGroup(_ group: PBXGroup) throws {
let hasDuplicatedChildren = group.children.count != Set(group.children).count
if hasDuplicatedChildren {
throw failure("Group \"\(group.nameOrPath)\" has duplicated children:\n - \(group.children.sorted().joined(separator: "\n - "))")
}
for child in group.children {
if let group = objects.groups.getReference(child) {
try validateGroup(group)
}
}
}
try validateGroup(mainGroup)
}
}
83 changes: 83 additions & 0 deletions Tests/XcodeGenKitTests/ProjectFixtureTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import PathKit
import ProjectSpec
import Spectre
import XcodeGenKit
import xcproj
import XCTest


class ProjectFixtureTests: XCTestCase {

func testProjectFixture() {
describe {
var xcodeProject: XcodeProj?

$0.it("generates") {
xcodeProject = try generateXcodeProject(specPath: fixturePath + "TestProject/project.yml", projectPath: fixturePath + "TestProject/Project.xcodeproj")
}

$0.it("generates variant group") {
guard let xcodeProject = xcodeProject else { return }

func getFileReferences(_ path: String) -> [ObjectReference<PBXFileReference>] {
return xcodeProject.pbxproj.objects.fileReferences.objectReferences.filter { $0.object.path == path }
}

func getVariableGroups(_ name: String?) -> [PBXVariantGroup] {
return xcodeProject.pbxproj.objects.variantGroups.referenceValues.filter { $0.name == name }
}

let resourceName = "LocalizedStoryboard.storyboard"
let baseResource = "Base.lproj/LocalizedStoryboard.storyboard"
let localizedResource = "en.lproj/LocalizedStoryboard.strings"

guard let variableGroup = getVariableGroups(resourceName).first else { throw failure("Couldn't find the variable group") }

do {
let refs = getFileReferences(baseResource)
try expect(refs.count) == 1
try expect(variableGroup.children.filter { $0 == refs.first?.reference }.count) == 1
}

do {
let refs = getFileReferences(localizedResource)
try expect(refs.count) == 1
try expect(variableGroup.children.filter { $0 == refs.first?.reference }.count) == 1
}
}

$0.it("generates scheme execution actions") {
guard let xcodeProject = xcodeProject else { return }

let frameworkScheme = xcodeProject.sharedData?.schemes.first { $0.name == "Framework" }
try expect(frameworkScheme?.buildAction?.preActions.first?.scriptText) == "echo Starting Framework Build"
try expect(frameworkScheme?.buildAction?.preActions.first?.title) == "Run Script"
try expect(frameworkScheme?.buildAction?.preActions.first?.environmentBuildable?.blueprintName) == "Framework_iOS"
try expect(frameworkScheme?.buildAction?.preActions.first?.environmentBuildable?.buildableName) == "Framework.framework"
}
}
}
}

fileprivate func generateXcodeProject(specPath: Path, projectPath: Path, file: String = #file, line: Int = #line) throws -> XcodeProj {
let project = try Project(path: specPath)
let generator = ProjectGenerator(project: project)
let xcodeProject = try generator.generateXcodeProject()
let oldProject = try XcodeProj(path: projectPath)
let pbxProjPath = projectPath + XcodeProj.pbxprojPath(projectPath)
let oldProjectString: String = try pbxProjPath.read()
try xcodeProject.write(path: projectPath, override: true)
let newProjectString: String = try pbxProjPath.read()

let newProject = try XcodeProj(path: projectPath)
let stringDiff = newProjectString != oldProjectString
if newProject != oldProject || stringDiff {
var message = "\(projectPath.string) has changed. If change is legitimate commit the change and run test again"
if stringDiff {
message += ":\n\n\(pbxProjPath):\n\(prettyFirstDifferenceBetweenStrings(oldProjectString, newProjectString))"
}
throw failure(message, file: file, line: line)
}

return newProject
}
Loading