-
Notifications
You must be signed in to change notification settings - Fork 1.4k
SE-0129 test naming conventions #566
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,25 +78,45 @@ extension ModuleError.InvalidLayoutType: FixableError { | |
| } | ||
|
|
||
| extension Module { | ||
| /// An error in the organization of an individual module. | ||
|
|
||
| /// An error in the organization or configuration of an individual module. | ||
| enum Error: Swift.Error { | ||
|
|
||
| /// The module's name is invalid. | ||
| case invalidName(path: String, name: String, problem: ModuleNameProblem) | ||
| enum ModuleNameProblem { | ||
| /// Empty module name. | ||
| case emptyName | ||
| /// Test module doesn't have a "Tests" suffix. | ||
| case noTestSuffix | ||
| /// Non-test module does have a "Tests" suffix. | ||
| case hasTestSuffix | ||
| } | ||
|
|
||
| /// The module contains no source code at all. | ||
| case noSources(String) | ||
|
|
||
| /// The module contains an invalid mix of languages (e.g. both Swift and C). | ||
| case mixedSources(String) | ||
| } | ||
| } | ||
|
|
||
| extension Module.Error: FixableError { | ||
| var error: String { | ||
| switch self { | ||
| case .noSources(let path): | ||
| case .invalidName(let path, let name, let problem): | ||
| return "the module at \(path) has an invalid name ('\(name)'): \(problem.error)" | ||
| case .noSources(let path): | ||
| return "the module at \(path) does not contain any source files" | ||
| case .mixedSources(let path): | ||
| case .mixedSources(let path): | ||
| return "the module at \(path) contains mixed language source files" | ||
| } | ||
| } | ||
|
|
||
| var fix: String? { | ||
| switch self { | ||
| case .invalidName(_, _, let problem): | ||
| return "rename the module (\(problem.fix))" | ||
| case .noSources(_): | ||
| return "either remove the module folder, or add a source file to the module" | ||
| case .mixedSources(_): | ||
|
|
@@ -105,6 +125,30 @@ extension Module.Error: FixableError { | |
| } | ||
| } | ||
|
|
||
| extension Module.Error.ModuleNameProblem : FixableError { | ||
| var error: String { | ||
| switch self { | ||
| case .emptyName: | ||
| return "the module name is empty" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm when will this be possible?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is only impossible today because the module name always comes from the file system name, but when validating the module name, it seems reasonable. If any higher level imposes an override. I can remove it until that happens, over course, so that we can get complete test coverage. Did I mention this PR is very much a work in progress. :-)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, makes sense.
Oops I was too excited 😄 |
||
| case .noTestSuffix: | ||
| return "the name of a test module has no ‘Tests’ suffix" | ||
| case .hasTestSuffix: | ||
| return "the name of a non-test module has a ‘Tests’ suffix" | ||
| } | ||
| } | ||
| var fix: String? { | ||
| switch self { | ||
| case .emptyName: | ||
| return "give the module a name" | ||
| case .noTestSuffix: | ||
| return "add a ‘Tests’ suffix" | ||
| case .hasTestSuffix: | ||
| return "remove the ‘Tests’ suffix" | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| extension Product { | ||
| /// An error in a product definition. | ||
| enum Error: Swift.Error { | ||
|
|
@@ -341,8 +385,26 @@ public struct PackageBuilder { | |
| return modules | ||
| } | ||
|
|
||
| /// Private function that checks whether a module name is valid. This method doesn't return anything, but rather, if there's a problem, it throws an error describing what the problem is. | ||
| // FIXME: We will eventually be loosening this restriction to allow test-only libraries etc | ||
| private func validateModuleName(_ path: AbsolutePath, _ name: String, isTest: Bool) throws { | ||
| if name.isEmpty { | ||
| throw Module.Error.invalidName(path: path.asString, name: name, problem: .emptyName) | ||
| } | ||
| if name.hasSuffix(Module.testModuleNameSuffix) && !isTest { | ||
| throw Module.Error.invalidName(path: path.asString, name: name, problem: .hasTestSuffix) | ||
| } | ||
| if !name.hasSuffix(Module.testModuleNameSuffix) && isTest { | ||
| throw Module.Error.invalidName(path: path.asString, name: name, problem: .noTestSuffix) | ||
| } | ||
| } | ||
|
|
||
| /// Private function that constructs a single Module object for the moduel at `path`, having the name `name`. If `isTest` is true, the module is constructed as a test module; if false, it is a regular module. | ||
| private func createModule(_ path: AbsolutePath, name: String, isTest: Bool) throws -> Module { | ||
|
|
||
| // Validate the module name. This function will throw an error if it detects a problem. | ||
| try validateModuleName(path, name, isTest: isTest) | ||
|
|
||
| // Find all the files under the module path. | ||
| let walked = try walk(path, fileSystem: fileSystem, recursing: shouldConsiderDirectory).map{ $0 } | ||
|
|
||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../FunctionalTests/Utilities.swift |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../FunctionalTests/Utilities.swift |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../FunctionalTests/Utilities.swift |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
invalidTestName/TestModuleNameErrorwould be clearer IMOThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on second look, this seems generic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, this is very much a work in progress. The idea is to overhaul the errors here so that they are specific, and this is definitely something that is going to need to grow over time. I will be adding comments and trying to make these errors (and their fix-its) as specific and clear as possible, but I see the testing module naming as just one aspect of various restrictions on the module name.