| // Copyright 2026 The Bazel Authors. All rights reserved. | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| use googletest::{expect_eq, expect_true, gtest}; | |
| use label::Label; | |
| #[gtest] | |
| fn parse_and_crate_name_accept_main_repo_labels() { | |
| expect_eq!(Label::parse("//src/rust/util:util").unwrap().crate_name().as_deref(), Ok("util")); | |
| expect_eq!(Label::parse("//src/rust/util").unwrap().crate_name().as_deref(), Ok("util")); | |
| expect_eq!(Label::parse("//:root_crate").unwrap().crate_name().as_deref(), Ok("root_crate")); | |
| expect_eq!(Label::parse("//pkg:my-crate").unwrap().crate_name().as_deref(), Ok("my_crate")); | |
| expect_eq!(Label::parse("//pkg:sub/crate").unwrap().crate_name().as_deref(), Ok("sub_crate")); | |
| } | |
| #[gtest] | |
| fn parse_accepts_valid_labels_whose_target_is_not_a_crate_identifier() { | |
| for valid_label in ["//pkg:foo.bar", "//pkg:123", "//pkg:_"] { | |
| let parsed = Label::parse(valid_label); | |
| expect_true!(parsed.is_ok(), "expected valid Bazel label `{valid_label}`"); | |
| expect_true!( | |
| parsed.unwrap().crate_name().is_err(), | |
| "expected crate_name error for `{valid_label}`" | |
| ); | |
| } | |
| } | |
| #[gtest] | |
| fn parse_rejects_invalid_labels() { | |
| for invalid in [ | |
| "", | |
| ":relative", | |
| "pkg:target", | |
| "//", | |
| "//pkg:", | |
| "//pkg:a:b", | |
| "//pkg:/foo", | |
| "//pkg:foo/", | |
| "//pkg:foo//bar", | |
| "//pkg://", | |
| "//pkg:.", | |
| "//pkg:..", | |
| "//pkg:../foo", | |
| "//pkg:foo/./bar", | |
| r"//pkg:a\b", | |
| "//pkg:a\nb", | |
| "//a//b:c", | |
| "//a/...:b", | |
| "//a/...", | |
| "//a/..../b:c", | |
| "//a/./b:c", | |
| "//a/../b:c", | |
| r"//src\rust\util:util", | |
| "//a\nb:c", | |
| "//a\x7fb:c", | |
| "//café:target", | |
| ] { | |
| expect_true!(Label::parse(invalid).is_err(), "expected error for label `{invalid}`"); | |
| } | |
| } |