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