Skip to content

bootstrap: add more unit tests #120172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add more unit tests
Signed-off-by: onur-ozkan <work@onurozkan.dev>
  • Loading branch information
onur-ozkan committed Jan 28, 2024
commit 76689539eec27faae941f5419983a0ebad93aad7
10 changes: 10 additions & 0 deletions src/bootstrap/src/tests/change_tracker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::{find_recent_config_change_ids, CONFIG_CHANGE_HISTORY};

#[test]
fn test_find_recent_config_change_ids() {
// If change-id is greater than the most recent one, result should be empty.
assert!(find_recent_config_change_ids(usize::MAX).is_empty());

// There is no change-id equal to or less than 0, result should include the entire change history.
assert_eq!(find_recent_config_change_ids(0).len(), CONFIG_CHANGE_HISTORY.len());
}
48 changes: 46 additions & 2 deletions src/bootstrap/src/tests/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use crate::utils::helpers::{check_cfg_arg, extract_beta_rev, hex_encode, make};
use std::path::PathBuf;
use crate::{
utils::helpers::{
check_cfg_arg, extract_beta_rev, hex_encode, make, program_out_of_date, symlink_dir,
},
Config,
};
use std::{
fs::{self, remove_file, File},
io::Write,
path::PathBuf,
};

#[test]
fn test_make() {
Expand Down Expand Up @@ -70,3 +79,38 @@ fn test_check_cfg_arg() {
"--check-cfg=cfg(target_os,values(\"nixos\",\"nix2\"))"
);
}

#[test]
fn test_program_out_of_date() {
let config = Config::parse(&["check".to_owned(), "--config=/does/not/exist".to_owned()]);
let tempfile = config.tempdir().join(".tmp-stamp-file");
File::create(&tempfile).unwrap().write_all(b"dummy value").unwrap();
assert!(tempfile.exists());

// up-to-date
assert!(!program_out_of_date(&tempfile, "dummy value"));
// out-of-date
assert!(program_out_of_date(&tempfile, ""));

remove_file(tempfile).unwrap();
}

#[test]
fn test_symlink_dir() {
let config = Config::parse(&["check".to_owned(), "--config=/does/not/exist".to_owned()]);
let tempdir = config.tempdir().join(".tmp-dir");
let link_path = config.tempdir().join(".tmp-link");

fs::create_dir_all(&tempdir).unwrap();
symlink_dir(&config, &tempdir, &link_path).unwrap();

let link_source = fs::read_link(&link_path).unwrap();
assert_eq!(link_source, tempdir);

fs::remove_dir(tempdir).unwrap();

#[cfg(windows)]
fs::remove_dir(link_path).unwrap();
#[cfg(not(windows))]
fs::remove_file(link_path).unwrap();
}
4 changes: 4 additions & 0 deletions src/bootstrap/src/utils/change_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
//! with the goal of keeping developers synchronized with important modifications in
//! the bootstrap.

#[cfg(test)]
#[path = "../tests/change_tracker.rs"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we doing this non-standard test movement? Just put the file in src/bootstrap/src/utils/change_tracker/test.rs -- that is the canonical way to add unit tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed the current approach in other bootstrap tests, but I agree. Will update it along with others.

mod tests;

#[derive(Clone, Debug)]
pub struct ChangeInfo {
/// Represents the ID of PR caused major change on bootstrap.
Expand Down