Skip to content

Commit

Permalink
Allow one-character module names
Browse files Browse the repository at this point in the history
Makes testing slightly easier.

PiperOrigin-RevId: 469962515
Change-Id: I67c6c5f5f732a80b0807d8580b4117f2d18c175e
  • Loading branch information
Wyverald authored and aiuto committed Oct 12, 2022
1 parent 3da0c80 commit 15ea7e4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@
public class ModuleFileGlobals {
/**
* A valid module name must: 1) begin with a lowercase letter; 2) end with a lowercase letter or a
* digit; 3) be at least 2 characters long; 4) contain only lowercase letters, digits, or one of
* '._-'.
* digit; 3) contain only lowercase letters, digits, or one of * '._-'.
*/
private static final Pattern VALID_MODULE_NAME = Pattern.compile("[a-z][a-z0-9._-]*[a-z0-9]");
private static final Pattern VALID_MODULE_NAME = Pattern.compile("[a-z]([a-z0-9._-]*[a-z0-9])?");

private boolean moduleCalled = false;
private final boolean ignoreDevDeps;
Expand Down Expand Up @@ -114,8 +113,7 @@ static void validateModuleName(String moduleName) throws EvalException {
throw Starlark.errorf(
"invalid module name '%s': valid names must 1) only contain lowercase letters (a-z),"
+ " digits (0-9), dots (.), hyphens (-), and underscores (_); 2) begin with a"
+ " lowercase letter; 3) end with a lowercase letter or digit; 4) be at least two"
+ " characters long.",
+ " lowercase letter; 3) end with a lowercase letter or digit.",
moduleName);
}
}
Expand All @@ -136,7 +134,7 @@ static void validateModuleName(String moduleName) throws EvalException {
+ " in, if it's not going to be depended on by another module). A valid module"
+ " name must: 1) only contain lowercase letters (a-z), digits (0-9), dots (.),"
+ " hyphens (-), and underscores (_); 2) begin with a lowercase letter; 3) end"
+ " with a lowercase letter or digit; 4) be at least two characters long.",
+ " with a lowercase letter or digit.",
named = true,
positional = false,
defaultValue = "''"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,9 @@ public void validateModuleName() throws Exception {
ModuleFileGlobals.validateModuleName("a3");
ModuleFileGlobals.validateModuleName("a.e");
ModuleFileGlobals.validateModuleName("a.-_e");
ModuleFileGlobals.validateModuleName("a");

assertThrows(EvalException.class, () -> ModuleFileGlobals.validateModuleName(""));
assertThrows(EvalException.class, () -> ModuleFileGlobals.validateModuleName("f"));
assertThrows(EvalException.class, () -> ModuleFileGlobals.validateModuleName("fooBar"));
assertThrows(EvalException.class, () -> ModuleFileGlobals.validateModuleName("_foo"));
assertThrows(EvalException.class, () -> ModuleFileGlobals.validateModuleName("foo#bar"));
Expand All @@ -655,24 +655,24 @@ public void validateModuleName() throws Exception {
public void badModuleName_module() throws Exception {
scratch.file(
rootDirectory.getRelative("MODULE.bazel").getPathString(),
"module(name='f',version='0.1')");
"module(name='f.',version='0.1')");

reporter.removeHandler(failFastHandler); // expect failures
evaluator.evaluate(ImmutableList.of(ModuleFileValue.KEY_FOR_ROOT_MODULE), evaluationContext);

assertContainsEvent("invalid module name 'f'");
assertContainsEvent("invalid module name 'f.'");
}

@Test
public void badModuleName_bazelDep() throws Exception {
scratch.file(
rootDirectory.getRelative("MODULE.bazel").getPathString(),
"bazel_dep(name='f',version='0.1')");
"bazel_dep(name='f.',version='0.1')");

reporter.removeHandler(failFastHandler); // expect failures
evaluator.evaluate(ImmutableList.of(ModuleFileValue.KEY_FOR_ROOT_MODULE), evaluationContext);

assertContainsEvent("invalid module name 'f'");
assertContainsEvent("invalid module name 'f.'");
}

@Test
Expand Down

0 comments on commit 15ea7e4

Please sign in to comment.