Skip to content

Commit 97a0566

Browse files
author
Miguel Perez
committed
Added tests
1 parent 49c6f95 commit 97a0566

File tree

17 files changed

+309
-29
lines changed

17 files changed

+309
-29
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// swift-tools-version:5.5
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "___NAME___",
8+
products: [
9+
// Products define the executables and libraries a package produces, and make them visible to other packages.
10+
.library(
11+
name: "___NAME___",
12+
targets: ["___NAME___"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
// .package(url: /* package url */, from: "1.0.0"),
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
21+
.target(
22+
name: "___NAME___",
23+
dependencies: [],
24+
path: "Sources/___NAME___"),
25+
.testTarget(
26+
name: "___NAME___Tests",
27+
dependencies: ["___NAME___"],
28+
path: "Tests/___NAME___Tests"),
29+
]
30+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ___NAME___
2+
3+
A description of this package.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public struct ___NAME___ {
2+
public private(set) var text = "Hello, World!"
3+
4+
public init() {
5+
}
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import XCTest
2+
@testable import ___NAME___
3+
4+
final class ___NAME___Tests: XCTestCase {
5+
func testExample() throws {
6+
// This is an example of a functional test case.
7+
// Use XCTAssert and related functions to verify your tests produce the correct
8+
// results.
9+
XCTAssertEqual(___NAME___().text, "Hello, World!")
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ___NAME___
2+
3+
A description of this package.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public struct ___NAME___ {
2+
public private(set) var text = "Hello, World!"
3+
4+
public init() {
5+
}
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import XCTest
2+
@testable import ___NAME___
3+
4+
final class ___NAME___Tests: XCTestCase {
5+
func testExample() throws {
6+
// This is an example of a functional test case.
7+
// Use XCTAssert and related functions to verify your tests produce the correct
8+
// results.
9+
XCTAssertEqual(___NAME___().text, "Hello, World!")
10+
}
11+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// swift-tools-version:5.5
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "___NAME___",
8+
products: [
9+
// Products define the executables and libraries a package produces, and make them visible to other packages.
10+
.library(
11+
name: "___NAME___",
12+
targets: ["___NAME___"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
// .package(url: /* package url */, from: "1.0.0"),
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
21+
.target(
22+
name: "___NAME___",
23+
dependencies: [],
24+
path: "Sources/___NAME___"),
25+
.testTarget(
26+
name: "___NAME___Tests",
27+
dependencies: ["___NAME___"],
28+
path: "Tests/___NAME___Tests"),
29+
]
30+
)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// swift-tools-version:5.5
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "___NAME___",
8+
products: [
9+
// Products define the executables and libraries a package produces, and make them visible to other packages.
10+
.library(
11+
name: "___NAME___",
12+
targets: ["___NAME___"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
// .package(url: /* package url */, from: "1.0.0"),
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20+
// Targets can depend on other targets in this package, and on products in packages this package depends on.
21+
.target(
22+
name: "___NAME___",
23+
dependencies: [],
24+
path: "Sources"),
25+
]
26+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ___NAME___
2+
3+
A description of this package.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public struct ___NAME___ {
2+
public private(set) var text = "Hello, World!"
3+
4+
public init() {
5+
}
6+
}

Sources/Commands/SwiftPackageTool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ extension SwiftPackageTool {
322322
throw StringError("\(path) is not a valid directory")
323323
}
324324

325-
templatePath = configPath.appending(components: "templates", "new-package", path.basename)
325+
templatePath = configPath.appending(components: "templates", "new-package", name ?? path.basename)
326326
try localFileSystem.copy(from: path, to: templatePath)
327327
} else {
328328
let provider = GitRepositoryProvider()

Sources/Workspace/InitPackage.swift

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,32 @@ public final class InitPackage {
5959
/// Where to create the new package
6060
let destinationPath: AbsolutePath
6161

62+
/// Filesystem used for writing and or copying the package structure
6263
let fileSystem: FileSystem
6364

6465
/// Configuration path for templates
6566
let configPath: AbsolutePath
6667

68+
/// Name for the package
6769
let packageName: String
6870

71+
/// If being called from `init` or `create`
6972
let mode: MakePackageMode
7073

74+
/// Type of package
7175
let packageType: InitPackage.PackageType
7276

77+
/// Name of the template to create the new package from
7378
let packageTemplateName: String?
7479

80+
let suppliedPackageType: Bool
81+
7582
private var moduleName: String {
76-
get {
77-
packageName.spm_mangledToC99ExtendedIdentifier()
78-
}
83+
get { packageName.spm_mangledToC99ExtendedIdentifier() }
7984
}
8085

8186
private var typeName: String {
82-
get {
83-
moduleName
84-
}
87+
get { moduleName }
8588
}
8689

8790
/// Create an instance that can create a package with given arguments.
@@ -104,10 +107,13 @@ public final class InitPackage {
104107
switch (packageType, mode){
105108
case (.some(let type), _):
106109
self.packageType = type
110+
self.suppliedPackageType = true
107111
case (.none, .initialize):
108112
self.packageType = .library
113+
self.suppliedPackageType = false
109114
case (.none, .create):
110115
self.packageType = .executable
116+
self.suppliedPackageType = false
111117
}
112118

113119
self.packageTemplateName = packageTemplateName
@@ -124,26 +130,32 @@ public final class InitPackage {
124130

125131
private func makePackageNew() throws {
126132
let templateHomeDirectory = configPath.appending(components: "templates", "new-package")
127-
var template: String?
133+
var templateName: String?
128134

129-
if let templateName = packageTemplateName {
130-
guard fileSystem.exists(templateHomeDirectory.appending(component: templateName)) else {
131-
throw InternalError("Could not find template folder: \(templateHomeDirectory.appending(component: templateName))")
135+
if let template = packageTemplateName {
136+
guard fileSystem.exists(templateHomeDirectory.appending(component: template)) else {
137+
throw InternalError("Could not find template folder: \(templateHomeDirectory.appending(component: template))")
132138
}
133139

134-
template = templateName
140+
templateName = template
135141
} else {
136142
// Checking if a default template is present
137-
if fileSystem.exists(templateHomeDirectory.appending(components: "default")) {
138-
template = "default"
143+
if fileSystem.exists(templateHomeDirectory.appending(component: "default")) {
144+
templateName = "default"
145+
// There is a guard preventing '--type' to be used in conjunction with '--template'
146+
// In the event that the defualt template is present and '--type' was used we'll infrom
147+
// the user that the package type is coming from the template and not their supplied type.
148+
if suppliedPackageType {
149+
progressReporter?("Package type is defined by the template 'default'.")
150+
}
139151
}
140152
}
141153

142-
if let templateName = template {
143-
try fileSystem.getDirectoryContents(templateHomeDirectory.appending(component: templateName)).forEach {
154+
if let template = templateName {
155+
try fileSystem.getDirectoryContents(templateHomeDirectory.appending(component: template)).forEach {
144156
progressReporter?("Copying \($0)")
145157
try copyTemplate(fileSystem: fileSystem,
146-
sourcePath: templateHomeDirectory.appending(components: templateName, $0),
158+
sourcePath: templateHomeDirectory.appending(components: template, $0),
147159
destinationPath: destinationPath,
148160
name: packageName)
149161
}
@@ -168,17 +180,22 @@ public final class InitPackage {
168180

169181
private func copyTemplate(fileSystem: FileSystem, sourcePath: AbsolutePath, destinationPath: AbsolutePath, name: String) throws {
170182
// Recursively copy the template package
171-
// Currently only replaces the string literal "$NAME"
183+
// Currently only replaces the string literal "___NAME___", and "___NAME_AS_C99___"
184+
let replaceName = "___NAME___"
185+
let replaceNameC99 = "___NAME_AS_C99___"
186+
172187
if fileSystem.isDirectory(sourcePath) {
173188
if let dirName = sourcePath.pathString.split(separator: "/").last {
174-
if !fileSystem.exists(destinationPath.appending(component: String(dirName))) {
175-
try fileSystem.createDirectory(destinationPath.appending(component: String(dirName)))
189+
190+
let newDirName = dirName.replacingOccurrences(of: replaceName, with: packageName)
191+
if !fileSystem.exists(destinationPath.appending(component: newDirName)) {
192+
try fileSystem.createDirectory(destinationPath.appending(component: newDirName))
176193
}
177194

178195
try fileSystem.getDirectoryContents(sourcePath).forEach {
179196
try copyTemplate(fileSystem: fileSystem,
180197
sourcePath: sourcePath.appending(component: $0),
181-
destinationPath: destinationPath.appending(components: String(dirName)),
198+
destinationPath: destinationPath.appending(components: newDirName),
182199
name: name)
183200
}
184201
}
@@ -187,18 +204,21 @@ public final class InitPackage {
187204

188205
if let validDescription = fileContents.validDescription {
189206
if let fileName = sourcePath.pathString.split(separator: "/").last {
190-
if !fileSystem.exists(destinationPath.appending(component: String(fileName))) {
191-
var renamed = validDescription.replacingOccurrences(of: "___NAME___", with: name)
192-
renamed = renamed.replacingOccurrences(of: "___NAME_AS_C99___", with: name.spm_mangledToC99ExtendedIdentifier())
207+
208+
let newFileName = fileName.replacingOccurrences(of: replaceName, with: packageName)
209+
if !fileSystem.exists(destinationPath.appending(component: newFileName)) {
210+
var renamed = validDescription.replacingOccurrences(of: replaceName, with: name)
211+
renamed = renamed.replacingOccurrences(of: replaceNameC99, with: name.spm_mangledToC99ExtendedIdentifier())
193212

194-
try fileSystem.writeFileContents(destinationPath.appending(component: String(fileName))) { $0 <<< renamed }
213+
try fileSystem.writeFileContents(destinationPath.appending(component: newFileName)) { $0 <<< renamed }
195214
}
196215
}
197216
} else {
198217
// This else takes care of things such as images
199218
if let fileName = sourcePath.pathString.split(separator: "/").last {
200-
if !fileSystem.exists(destinationPath.appending(component: String(fileName))) {
201-
try fileSystem.copy(from: sourcePath, to: destinationPath.appending(component: String(fileName)))
219+
let newFileName = fileName.replacingOccurrences(of: replaceName, with: packageName)
220+
if !fileSystem.exists(destinationPath.appending(component: newFileName)) {
221+
try fileSystem.copy(from: sourcePath, to: destinationPath.appending(component: newFileName))
202222
}
203223
}
204224
}

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,9 @@ final class PackageToolTests: XCTestCase {
584584
let fs = localFileSystem
585585
let path = tmpPath.appending(component: "MyExe")
586586

587-
_ = try execute(["create", "MyExe"], packagePath: tmpPath)
587+
// Here --config-path is used to avoid a situation where the 'default' template
588+
// is present
589+
_ = try execute(["create", "MyExe", "--config-path", tmpPath.pathString], packagePath: tmpPath)
588590

589591
XCTAssert(fs.exists(path.appending(component: "Package.swift")))
590592
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources")), ["main.swift"])
@@ -597,7 +599,9 @@ final class PackageToolTests: XCTestCase {
597599
let fs = localFileSystem
598600
let path = tmpPath.appending(component: "MyLib")
599601

600-
_ = try execute(["create", "MyLib", "--type", "library"], packagePath: tmpPath)
602+
// Here --config-path is used to avoid a situation where the 'default' template
603+
// is present
604+
_ = try execute(["create", "MyLib", "--type", "library", "--config-path", tmpPath.pathString], packagePath: tmpPath)
601605

602606
XCTAssert(fs.exists(path.appending(component: "Package.swift")))
603607
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources")), ["MyLib.swift"])

0 commit comments

Comments
 (0)