Skip to content

Commit 9f71231

Browse files
committed
Add global_cache_tracker stability tests.
This adds some tests to ensure that the database used in the global cache tracker stays compatible across versions. These tests work by using rustup to run both the current cargo and the stable cargo, and verifying that when switching versions, everything works as expected.
1 parent 8bb6231 commit 9f71231

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ jobs:
156156
- uses: actions/checkout@v4
157157
- name: Dump Environment
158158
run: ci/dump-environment.sh
159+
# Some tests require stable. Make sure it is set to the most recent stable
160+
# so that we can predictably handle updates if necessary (and not randomly
161+
# when GitHub updates its image).
162+
- run: rustup update --no-self-update stable
159163
- run: rustup update --no-self-update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
160164
- run: rustup target add ${{ matrix.other }}
161165
- run: rustup component add rustc-dev llvm-tools-preview rust-docs

tests/testsuite/global_cache_tracker.rs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,3 +1863,152 @@ fn clean_gc_quiet_is_quiet() {
18631863
)
18641864
.run();
18651865
}
1866+
1867+
#[cargo_test(requires_rustup_stable)]
1868+
fn compatible_with_older_cargo() {
1869+
// Ensures that db stays backwards compatible across versions.
1870+
1871+
// T-4 months: Current version, build the database.
1872+
Package::new("old", "1.0.0").publish();
1873+
Package::new("middle", "1.0.0").publish();
1874+
Package::new("new", "1.0.0").publish();
1875+
let p = project()
1876+
.file(
1877+
"Cargo.toml",
1878+
r#"
1879+
[package]
1880+
name = "foo"
1881+
version = "0.1.0"
1882+
1883+
[dependencies]
1884+
old = "1.0"
1885+
middle = "1.0"
1886+
new = "1.0"
1887+
"#,
1888+
)
1889+
.file("src/lib.rs", "")
1890+
.build();
1891+
// Populate the last-use data.
1892+
p.cargo("check -Zgc")
1893+
.masquerade_as_nightly_cargo(&["gc"])
1894+
.env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(4))
1895+
.run();
1896+
assert_eq!(
1897+
get_registry_names("src"),
1898+
["middle-1.0.0", "new-1.0.0", "old-1.0.0"]
1899+
);
1900+
assert_eq!(
1901+
get_registry_names("cache"),
1902+
["middle-1.0.0.crate", "new-1.0.0.crate", "old-1.0.0.crate"]
1903+
);
1904+
1905+
// T-2 months: Stable version, make sure it reads and deletes old src.
1906+
p.change_file(
1907+
"Cargo.toml",
1908+
r#"
1909+
[package]
1910+
name = "foo"
1911+
version = "0.1.0"
1912+
1913+
[dependencies]
1914+
new = "1.0"
1915+
middle = "1.0"
1916+
"#,
1917+
);
1918+
p.process("cargo")
1919+
.args(&["+stable", "check", "-Zgc"])
1920+
.masquerade_as_nightly_cargo(&["gc"])
1921+
.env("__CARGO_TEST_LAST_USE_NOW", months_ago_unix(2))
1922+
// Necessary since `process` removes rustup.
1923+
.env("PATH", std::env::var_os("PATH").unwrap())
1924+
.run();
1925+
assert_eq!(get_registry_names("src"), ["middle-1.0.0", "new-1.0.0"]);
1926+
assert_eq!(
1927+
get_registry_names("cache"),
1928+
["middle-1.0.0.crate", "new-1.0.0.crate", "old-1.0.0.crate"]
1929+
);
1930+
1931+
// T-0 months: Current version, make sure it can read data from stable,
1932+
// deletes old crate and middle src.
1933+
p.change_file(
1934+
"Cargo.toml",
1935+
r#"
1936+
[package]
1937+
name = "foo"
1938+
version = "0.1.0"
1939+
1940+
[dependencies]
1941+
new = "1.0"
1942+
"#,
1943+
);
1944+
p.cargo("check -Zgc")
1945+
.masquerade_as_nightly_cargo(&["gc"])
1946+
.run();
1947+
assert_eq!(get_registry_names("src"), ["new-1.0.0"]);
1948+
assert_eq!(
1949+
get_registry_names("cache"),
1950+
["middle-1.0.0.crate", "new-1.0.0.crate"]
1951+
);
1952+
}
1953+
1954+
#[cargo_test(requires_rustup_stable)]
1955+
fn forward_compatible() {
1956+
// Checks that db created in an older version can be read in a newer version.
1957+
Package::new("bar", "1.0.0").publish();
1958+
let git_project = git::new("from_git", |p| {
1959+
p.file("Cargo.toml", &basic_manifest("from_git", "1.0.0"))
1960+
.file("src/lib.rs", "")
1961+
});
1962+
1963+
let p = project()
1964+
.file(
1965+
"Cargo.toml",
1966+
&format!(
1967+
r#"
1968+
[package]
1969+
name = "foo"
1970+
1971+
[dependencies]
1972+
bar = "1.0.0"
1973+
from_git = {{ git = '{}' }}
1974+
"#,
1975+
git_project.url()
1976+
),
1977+
)
1978+
.file("src/lib.rs", "")
1979+
.build();
1980+
1981+
p.process("cargo")
1982+
.args(&["+stable", "check", "-Zgc"])
1983+
.masquerade_as_nightly_cargo(&["gc"])
1984+
// Necessary since `process` removes rustup.
1985+
.env("PATH", std::env::var_os("PATH").unwrap())
1986+
.run();
1987+
1988+
let config = GlobalContextBuilder::new().unstable_flag("gc").build();
1989+
let lock = config
1990+
.acquire_package_cache_lock(CacheLockMode::MutateExclusive)
1991+
.unwrap();
1992+
let tracker = GlobalCacheTracker::new(&config).unwrap();
1993+
// Don't want to check the actual index name here, since although the
1994+
// names are semi-stable, they might change over long periods of time.
1995+
let indexes = tracker.registry_index_all().unwrap();
1996+
assert_eq!(indexes.len(), 1);
1997+
let crates = tracker.registry_crate_all().unwrap();
1998+
let names: Vec<_> = crates
1999+
.iter()
2000+
.map(|(krate, _timestamp)| krate.crate_filename)
2001+
.collect();
2002+
assert_eq!(names, &["bar-1.0.0.crate"]);
2003+
let srcs = tracker.registry_src_all().unwrap();
2004+
let names: Vec<_> = srcs
2005+
.iter()
2006+
.map(|(src, _timestamp)| src.package_dir)
2007+
.collect();
2008+
assert_eq!(names, &["bar-1.0.0"]);
2009+
let dbs: Vec<_> = tracker.git_db_all().unwrap();
2010+
assert_eq!(dbs.len(), 1);
2011+
let cos: Vec<_> = tracker.git_checkout_all().unwrap();
2012+
assert_eq!(cos.len(), 1);
2013+
drop(lock);
2014+
}

0 commit comments

Comments
 (0)