|
| 1 | +// Unit tests for the pure helpers the phases of prepare_build share |
| 2 | +// (src/build/prepare/config.cpp and fetch.cpp): the --features request |
| 3 | +// grammar, the macro name a define entry names, the previous release's |
| 4 | +// reading of a flag element, and whether a git remote is local. Before the |
| 5 | +// decomposition of prepare.cppm these were internal to one 16,000-line unit |
| 6 | +// and reached only through whole builds; they are exported for this file. |
| 7 | + |
| 8 | +#include <gtest/gtest.h> |
| 9 | + |
| 10 | +import std; |
| 11 | +import mcpp.build.prepare; |
| 12 | + |
| 13 | +using Words = std::vector<std::string>; |
| 14 | + |
| 15 | +TEST(FeatureRequest, TokensSplitOnCommasAndSpaces) { |
| 16 | + EXPECT_EQ(mcpp::build::feature_request_tokens("a,b c,, d"), (Words{"a", "b", "c", "d"})); |
| 17 | + EXPECT_TRUE(mcpp::build::feature_request_tokens("").empty()); |
| 18 | + EXPECT_TRUE(mcpp::build::feature_request_tokens(" , ").empty()); |
| 19 | +} |
| 20 | + |
| 21 | +// #649 E8: a token with `/` opens a dependency's feature, so it is a forward |
| 22 | +// of the root and never one of the root's own features. A token with an |
| 23 | +// empty half stays whole, for the caller to name. |
| 24 | +TEST(FeatureRequest, ASlashTokenIsAForwardAndNotARootFeature) { |
| 25 | + constexpr std::string_view request = "simd, dep/fast, gpu,/x,y/"; |
| 26 | + EXPECT_EQ(mcpp::build::parse_feature_request(request), (Words{"simd", "gpu"})); |
| 27 | + EXPECT_EQ(mcpp::build::feature_forward_request_tokens(request), |
| 28 | + (Words{"dep/fast", "/x", "y/"})); |
| 29 | +} |
| 30 | + |
| 31 | +TEST(DefineName, IsTheTextBeforeTheFirstEquals) { |
| 32 | + EXPECT_EQ(mcpp::build::define_name("NDEBUG"), "NDEBUG"); |
| 33 | + EXPECT_EQ(mcpp::build::define_name("VERSION=1"), "VERSION"); |
| 34 | + EXPECT_EQ(mcpp::build::define_name("EXPR=a=b"), "EXPR"); |
| 35 | + EXPECT_EQ(mcpp::build::define_name("=x"), ""); |
| 36 | +} |
| 37 | + |
| 38 | +// The words an element reached the compiler as before flag_words: `$$` was a |
| 39 | +// literal dollar, and `${name}` or `$name` a ninja variable, empty on a |
| 40 | +// compile edge. A `defines` entry was read as `-D<entry>`. |
| 41 | +TEST(PreviousReleaseWords, ReadNinjaEscapesAndVariablesAsTheOldEdgeDid) { |
| 42 | + EXPECT_EQ(mcpp::build::previous_release_words("-DA=x$$y", false), (Words{"-DA=x$y"})); |
| 43 | + EXPECT_EQ(mcpp::build::previous_release_words("-DA=${v}z", false), (Words{"-DA=z"})); |
| 44 | + EXPECT_EQ(mcpp::build::previous_release_words("-DA=$v", false), (Words{"-DA="})); |
| 45 | + EXPECT_EQ(mcpp::build::previous_release_words("B=1", true), (Words{"-DB=1"})); |
| 46 | +} |
| 47 | + |
| 48 | +// A remote served by plain filesystem reads is local, so `--offline` keeps |
| 49 | +// building it; a scheme or scp-like syntax is remote. A Windows drive letter |
| 50 | +// has a colon but no `@`, so an existing `C:\repo` stays local. |
| 51 | +TEST(GitRemote, LocalMeansAFileUrlOrAnExistingPath) { |
| 52 | + EXPECT_TRUE(mcpp::build::is_local_git_remote("file:///srv/repo.git")); |
| 53 | + EXPECT_FALSE(mcpp::build::is_local_git_remote("https://github.com/x/y.git")); |
| 54 | + EXPECT_FALSE(mcpp::build::is_local_git_remote("ssh://git@host/x.git")); |
| 55 | + EXPECT_FALSE(mcpp::build::is_local_git_remote("git@github.com:x/y.git")); |
| 56 | + |
| 57 | + namespace fs = std::filesystem; |
| 58 | + const auto dir = fs::temp_directory_path() |
| 59 | + / std::format("mcpp-prepare-helpers-{:x}", std::random_device{}()); |
| 60 | + fs::create_directories(dir); |
| 61 | + EXPECT_TRUE(mcpp::build::is_local_git_remote(dir.string())); |
| 62 | + EXPECT_FALSE(mcpp::build::is_local_git_remote((dir / "missing").string())); |
| 63 | + std::error_code ec; |
| 64 | + fs::remove_all(dir, ec); |
| 65 | +} |
0 commit comments