Skip to content

Commit 1f11273

Browse files
author
Miguel Perez
committed
Added tests
1 parent 1fdaec6 commit 1f11273

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
@@ -60,29 +60,32 @@ public final class InitPackage {
6060
/// Where to create the new package
6161
let destinationPath: AbsolutePath
6262

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

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

69+
/// Name for the package
6870
let packageName: String
6971

72+
/// If being called from `init` or `create`
7073
let mode: MakePackageMode
7174

75+
/// Type of package
7276
let packageType: InitPackage.PackageType
7377

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

81+
let suppliedPackageType: Bool
82+
7683
private var moduleName: String {
77-
get {
78-
packageName.spm_mangledToC99ExtendedIdentifier()
79-
}
84+
get { packageName.spm_mangledToC99ExtendedIdentifier() }
8085
}
8186

8287
private var typeName: String {
83-
get {
84-
moduleName
85-
}
88+
get { moduleName }
8689
}
8790

8891
/// Create an instance that can create a package with given arguments.
@@ -105,10 +108,13 @@ public final class InitPackage {
105108
switch (packageType, mode){
106109
case (.some(let type), _):
107110
self.packageType = type
111+
self.suppliedPackageType = true
108112
case (.none, .initialize):
109113
self.packageType = .library
114+
self.suppliedPackageType = false
110115
case (.none, .create):
111116
self.packageType = .executable
117+
self.suppliedPackageType = false
112118
}
113119

114120
self.packageTemplateName = packageTemplateName
@@ -125,26 +131,32 @@ public final class InitPackage {
125131

126132
private func makePackageNew() throws {
127133
let templateHomeDirectory = configPath.appending(components: "templates", "new-package")
128-
var template: String?
134+
var templateName: String?
129135

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

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

143-
if let templateName = template {
144-
try fileSystem.getDirectoryContents(templateHomeDirectory.appending(component: templateName)).forEach {
155+
if let template = templateName {
156+
try fileSystem.getDirectoryContents(templateHomeDirectory.appending(component: template)).forEach {
145157
progressReporter?("Copying \($0)")
146158
try copyTemplate(fileSystem: fileSystem,
147-
sourcePath: templateHomeDirectory.appending(components: templateName, $0),
159+
sourcePath: templateHomeDirectory.appending(components: template, $0),
148160
destinationPath: destinationPath,
149161
name: packageName)
150162
}
@@ -169,17 +181,22 @@ public final class InitPackage {
169181

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

179196
try fileSystem.getDirectoryContents(sourcePath).forEach {
180197
try copyTemplate(fileSystem: fileSystem,
181198
sourcePath: sourcePath.appending(component: $0),
182-
destinationPath: destinationPath.appending(components: String(dirName)),
199+
destinationPath: destinationPath.appending(components: newDirName),
183200
name: name)
184201
}
185202
}
@@ -188,18 +205,21 @@ public final class InitPackage {
188205

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

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

Tests/CommandsTests/PackageToolTests.swift

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

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

521523
XCTAssert(fs.exists(path.appending(component: "Package.swift")))
522524
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources")), ["main.swift"])
@@ -529,7 +531,9 @@ final class PackageToolTests: XCTestCase {
529531
let fs = localFileSystem
530532
let path = tmpPath.appending(component: "MyLib")
531533

532-
_ = try execute(["create", "MyLib", "--type", "library"], packagePath: tmpPath)
534+
// Here --config-path is used to avoid a situation where the 'default' template
535+
// is present
536+
_ = try execute(["create", "MyLib", "--type", "library", "--config-path", tmpPath.pathString], packagePath: tmpPath)
533537

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

0 commit comments

Comments
 (0)