Repository: carbon-language/carbon-lang
File: toolchain/install/busybox_info_test.cpp (C++)

Code before:
  // - `.../usr/bin/../lib/carbon/carbon-busybox`
  auto stray_target = MakeSymlink(dir_ / "usr/local/carbon", "bin/carbon");

  // Check that the busybox doesn't use the relative busybox in this case, and
  // walks the symlink to find the correct installation.
  auto info = GetBusyboxInfo(stray_target.c_str());
  ASSERT_TRUE(info.ok()) << info.error();
  EXPECT_THAT(info->bin_path,
              Eq(dir_ / "usr/local/bin/../lib/carbon/carbon-busybox"));

  // Ensure this works even with intervening `.` directory components.
  stray_target = MakeSymlink(dir_ / "usr/local/carbon2", "bin/././carbon");

  // Check that the busybox doesn't use the relative busybox in this case, and
  // walks the symlink to find the correct installation.
  info = GetBusyboxInfo(stray_target.c_str());
  ASSERT_TRUE(info.ok()) << info.error();
  EXPECT_THAT(info->bin_path,
              Eq(dir_ / "usr/local/bin/../lib/carbon/carbon-busybox"));
}

TEST_F(BusyboxInfoTest, EnvBinaryPathOverride) {
  // The test should not have this environment variable set.
  ASSERT_THAT(getenv(Argv0OverrideEnv), Eq(nullptr));
  // Clean up this environment variable when this test finishes.
  auto _ = llvm::make_scope_exit([] { unsetenv(Argv0OverrideEnv); });

  // Set the environment to our actual busybox.
  auto busybox = MakeBusyboxFile(dir_ / "carbon-busybox");
  setenv(Argv0OverrideEnv, busybox.c_str(), /*overwrite=*/1);

  auto info = GetBusyboxInfo("/some/nonexistent/path");
  ASSERT_TRUE(info.ok()) << info.error();
  EXPECT_THAT(info->bin_path, Eq(busybox));
  EXPECT_THAT(info->mode, Eq(std::nullopt));
}

}  // namespace
}  // namespace Carbon


Review comment (bug):
I guess you can drop this now (or better, test that `getenv(Argv0OverrideEnv) == nullptr` after calling `GetBusyboxInfo`)

Code after:
  // - `.../usr/bin/../lib/carbon/carbon-busybox`
  auto stray_target = MakeSymlink(dir_ / "usr/local/carbon", "bin/carbon");

  // Check that the busybox doesn't use the relative busybox in this case, and
  // walks the symlink to find the correct installation.
  auto info = GetBusyboxInfo(stray_target.c_str());
  ASSERT_TRUE(info.ok()) << info.error();
  EXPECT_THAT(info->bin_path,
              Eq(dir_ / "usr/local/bin/../lib/carbon/carbon-busybox"));

  // Ensure this works even with intervening `.` directory components.
  stray_target = MakeSymlink(dir_ / "usr/local/carbon2", "bin/././carbon");

  // Check that the busybox doesn't use the relative busybox in this case, and
  // walks the symlink to find the correct installation.
  info = GetBusyboxInfo(stray_target.c_str());
  ASSERT_TRUE(info.ok()) << info.error();
  EXPECT_THAT(info->bin_path,
              Eq(dir_ / "usr/local/bin/../lib/carbon/carbon-busybox"));
}

TEST_F(BusyboxInfoTest, EnvBinaryPathOverride) {
  // The test should not have this environment variable set.
  ASSERT_THAT(getenv(Argv0OverrideEnv), Eq(nullptr));

  // Set the environment to our actual busybox.
  auto busybox = MakeBusyboxFile(dir_ / "carbon-busybox");

  setenv(Argv0OverrideEnv, busybox.c_str(), /*overwrite=*/1);
  auto info = GetBusyboxInfo("/some/nonexistent/path");
  if (getenv(Argv0OverrideEnv)) {
    unsetenv(Argv0OverrideEnv);
    ADD_FAILURE() << "GetBusyboxInfo should unset Argv0OverrideEnv";
  }

  ASSERT_TRUE(info.ok()) << info.error();
  EXPECT_THAT(info->bin_path, Eq(busybox));
  EXPECT_THAT(info->mode, Eq(std::nullopt));
}

}  // namespace
}  // namespace Carbon