Skip to content

Commit

Permalink
test(global_cache_tracker): verify workspace manifest and target dir …
Browse files Browse the repository at this point in the history
…recorded correctly
  • Loading branch information
baby230211 committed May 23, 2024
1 parent 0eb4ca7 commit 481de08
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/cargo/core/global_cache_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,42 @@ impl GlobalCacheTracker {
Ok(rows)
}

// Return all workspace_manifest cache timestamps.
pub fn workspace_manifest_all(&self) -> CargoResult<Vec<(WorkspaceManifestIndex, Timestamp)>> {
let mut stmt = self
.conn
.prepare_cached("SELECT name, timestamp FROM workspace_manifest_index")?;
let rows = stmt
.query_map([], |row| {
let workspace_manifest_path = row.get_unwrap(0);
let timestamp = row.get_unwrap(1);
let kind = WorkspaceManifestIndex {
workspace_manifest_path: workspace_manifest_path,
};
Ok((kind, timestamp))
})?
.collect::<Result<Vec<_>, _>>()?;
Ok(rows)
}

// Return all target dir cache timestamps.
pub fn target_dir_all(&self) -> CargoResult<Vec<(TargetDirIndex, Timestamp)>> {
let mut stmt = self
.conn
.prepare_cached("SELECT name, timestamp FROM target_dir_index")?;
let rows = stmt
.query_map([], |row| {
let target_dir_path = row.get_unwrap(0);
let timestamp = row.get_unwrap(1);
let kind = TargetDirIndex {
target_dir_path: target_dir_path,
};
Ok((kind, timestamp))
})?
.collect::<Result<Vec<_>, _>>()?;
Ok(rows)
}

/// Returns whether or not an auto GC should be performed, compared to the
/// last time it was recorded in the database.
pub fn should_run_auto_gc(&mut self, frequency: Duration) -> CargoResult<bool> {
Expand Down
15 changes: 15 additions & 0 deletions tests/testsuite/global_cache_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ fn implies_source() {
short_name: "f0a4ee0".into(),
size: None,
});
deferred.mark_workspace_src_used(global_cache_tracker::WorkspaceSrc {
target_dir_path: "/Users/foo/cargo/target".into(),
workspace_manifest_path: "/Users/foo/cargo/Cargo.toml".into(),
});
deferred.save(&mut tracker).unwrap();

let mut indexes = tracker.registry_index_all().unwrap();
Expand All @@ -240,6 +244,17 @@ fn implies_source() {
let dbs = tracker.git_db_all().unwrap();
assert_eq!(dbs.len(), 1);
assert_eq!(dbs[0].0.encoded_git_name, "cargo-e7ff1db891893a9e");

let workspace_manifests = tracker.workspace_manifest_all().unwrap();
assert_eq!(workspace_manifests.len(), 1);
assert_eq!(
workspace_manifests[0].0.workspace_manifest_path,
"/Users/foo/cargo/Cargo.toml"
);

let target_dirs = tracker.target_dir_all().unwrap();
assert_eq!(target_dirs.len(), 1);
assert_eq!(target_dirs[0].0.target_dir_path, "/Users/foo/cargo/target");
}

#[cargo_test]
Expand Down

0 comments on commit 481de08

Please sign in to comment.