github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/core/label_parse_test.go (about)

     1  // Tests parsing of build labels.
     2  
     3  package core
     4  
     5  import "testing"
     6  
     7  func assertLabel(t *testing.T, in, pkg, name string) {
     8  	defer func() {
     9  		if r := recover(); r != nil {
    10  			t.Errorf("Failed to parse %s: %s", in, r)
    11  		}
    12  	}()
    13  	label := ParseBuildLabel(in, "current_package")
    14  	if label.PackageName != pkg {
    15  		t.Errorf("Incorrect parse of %s: package name should be %s, was %s", in, pkg, label.PackageName)
    16  	}
    17  	if label.Name != name {
    18  		t.Errorf("Incorrect parse of %s: target name should be %s, was %s", in, name, label.Name)
    19  	}
    20  }
    21  
    22  func assertRelativeLabel(t *testing.T, in, pkg, name string) {
    23  	if label, err := parseMaybeRelativeBuildLabel(in, "current_package"); err != nil {
    24  		t.Errorf("Failed to parse %s: %s", in, err)
    25  	} else if label.PackageName != pkg {
    26  		t.Errorf("Incorrect parse of %s: package name should be %s, was %s", in, pkg, label.PackageName)
    27  	} else if label.Name != name {
    28  		t.Errorf("Incorrect parse of %s: target name should be %s, was %s", in, name, label.Name)
    29  	}
    30  }
    31  
    32  func assertNotLabel(t *testing.T, in, reason string) {
    33  	var label BuildLabel
    34  	defer func() {
    35  		if r := recover(); r == nil {
    36  			t.Errorf("%s should have failed (%s), instead generated %s", in, reason, label)
    37  		}
    38  	}()
    39  	label = ParseBuildLabel(in, "current_package")
    40  }
    41  
    42  // These labels are accepted anywhere, on the command line or in BUILD files.
    43  
    44  func TestAbsoluteTarget(t *testing.T) {
    45  	assertLabel(t, "//path/to:target", "path/to", "target")
    46  	assertLabel(t, "//path:target", "path", "target")
    47  	assertLabel(t, "//:target", "", "target")
    48  	assertNotLabel(t, "//path:to/target", "can't have slashes in target names")
    49  	assertNotLabel(t, "//path:to:target", "can't have multiple colons")
    50  	assertNotLabel(t, "/path:to/target", "must have two initial slashes")
    51  	assertNotLabel(t, "/path/to:", "must pass a target name")
    52  }
    53  
    54  func TestLocalTarget(t *testing.T) {
    55  	assertLabel(t, ":target", "current_package", "target")
    56  	assertLabel(t, ":thingy_wotsit_123", "current_package", "thingy_wotsit_123")
    57  	assertNotLabel(t, ":to/target", "can't have slashes in target names")
    58  	assertNotLabel(t, ":to:target", "can't have multiple colons")
    59  	assertNotLabel(t, "::to_target", "can't have multiple colons")
    60  }
    61  
    62  func TestImplicitTarget(t *testing.T) {
    63  	assertLabel(t, "//path/to", "path/to", "to")
    64  	assertLabel(t, "//path", "path", "path")
    65  	assertNotLabel(t, "/path", "must have two initial slashes")
    66  }
    67  
    68  func TestSubTargets(t *testing.T) {
    69  	assertLabel(t, "//path/to/...", "path/to", "...")
    70  	assertLabel(t, "//path/...", "path", "...")
    71  	assertLabel(t, "//...", "", "...")
    72  	// These three are not passing at the moment. Not completely crucial since the ... will just be
    73  	// treated as a package name but would be nice if they were rejected here.
    74  	// assertNotLabel(t, "//...:hello", "can't have stuff after the ellipsis")
    75  	// assertNotLabel(t, "//...1234", "can't have stuff after the ellipsis")
    76  	// assertNotLabel(t, "//.../...", "can't have multiple ellipses")
    77  }
    78  
    79  // The following are only accepted on the command line and converted to absolute
    80  // labels based on the current directory.
    81  
    82  func TestRelativeSubTargets(t *testing.T) {
    83  	assertRelativeLabel(t, "...", "current_package", "...")
    84  	assertRelativeLabel(t, "path/to/...", "current_package/path/to", "...")
    85  	assertNotLabel(t, "...:hello", "can't have stuff after the ellipsis")
    86  	assertNotLabel(t, "...1234", "can't have stuff after the ellipsis")
    87  	assertNotLabel(t, ".../...", "can't have multiple ellipses")
    88  }
    89  
    90  func TestRelativeTarget(t *testing.T) {
    91  	assertRelativeLabel(t, "path/to:thingy", "current_package/path/to", "thingy")
    92  	assertRelativeLabel(t, ":thingy", "current_package", "thingy")
    93  	assertNotLabel(t, "path/to:", "must have a target name")
    94  	assertNotLabel(t, "path/to:thingy/mabob", "can't have a slash in target name")
    95  	assertNotLabel(t, "path/to:thingy:mabob", "can only have one colon")
    96  }
    97  
    98  func TestRelativeImplicitTarget(t *testing.T) {
    99  	assertRelativeLabel(t, "path/to", "current_package/path/to", "to")
   100  	assertRelativeLabel(t, "path", "current_package/path", "path")
   101  	assertNotLabel(t, "path/to:", "must have a target name")
   102  }
   103  
   104  // Test for issue #55 where we were incorrectly allowing consecutive double slashes,
   105  // which has all manner of weird follow-on effects
   106  func TestDoubleSlashes(t *testing.T) {
   107  	assertNotLabel(t, "//src//core", "double slashes not allowed")
   108  	assertNotLabel(t, "//src//core:target1", "double slashes not allowed")
   109  	assertNotLabel(t, "//src/core/something//something", "double slashes not allowed")
   110  }
   111  
   112  // Test that labels can't match reserved suffixes used for temp dirs.
   113  func TestReservedTempDirs(t *testing.T) {
   114  	assertNotLabel(t, "//src/core:core._build", "._build is a reserved suffix")
   115  	assertNotLabel(t, "//src/core:core._test", "._test is a reserved suffix")
   116  }
   117  
   118  func TestNonAsciiParse(t *testing.T) {
   119  	assertLabel(t, "//src/core:aerolínea", "src/core", "aerolínea")
   120  }
   121  
   122  func TestDotsArentAccepted(t *testing.T) {
   123  	assertNotLabel(t, "//src/core:.", ". is not a valid label name")
   124  	assertNotLabel(t, "//src/core:..", ".. is not a valid label name")
   125  	assertNotLabel(t, "//src/core:...", "... is not a valid label name")
   126  	assertNotLabel(t, "//src/core:....", ".... is not a valid label name")
   127  	assertLabel(t, "//src/core/...", "src/core", "...")
   128  }
   129  
   130  func TestPipesArentAccepted(t *testing.T) {
   131  	assertNotLabel(t, "//src/core:core|build_label.go", "| is not allowed in build labels")
   132  }
   133  
   134  func TestSubrepos(t *testing.T) {
   135  	assertLabel(t, "@subrepo//pkg:target", "subrepo/pkg", "target")
   136  	assertLabel(t, "@com_google_googletest//:gtest_main", "com_google_googletest", "gtest_main")
   137  	assertLabel(t, "@test_x86:target", "test_x86/current_package", "target")
   138  }