Skip to content

Commit

Permalink
🧪 - Add tests and improve test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrolich committed Jul 24, 2023
1 parent a5789b4 commit efb0151
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 91 deletions.
21 changes: 11 additions & 10 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,11 +1207,12 @@ fn compute_file_hash(path: &str) -> Option<blake3::Hash> {
}
}

pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()> {
pub fn build(filter: &Option<regex::Regex>, path: &str, no_timing: bool) -> Result<BuildState, ()> {
let timing_total = Instant::now();
let project_root = helpers::get_abs_path(path);
let rescript_version = get_version(&project_root);
let root_config_name = package_tree::get_package_name(&project_root);
let default_timing: Option<std::time::Duration> = if no_timing { Some(std::time::Duration::new(0.0 as u64, 0.0 as u32)) } else { None };

print!(
"{} {} Building package tree...",
Expand All @@ -1229,7 +1230,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
LINE_CLEAR,
style("[1/7]").bold().dim(),
CHECKMARK,
timing_package_tree_elapsed.as_secs_f64()
default_timing.unwrap_or(timing_package_tree_elapsed).as_secs_f64()
);

let timing_source_files = Instant::now();
Expand All @@ -1247,7 +1248,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
LINE_CLEAR,
style("[2/7]").bold().dim(),
CHECKMARK,
timing_source_files_elapsed.as_secs_f64()
default_timing.unwrap_or(timing_source_files_elapsed).as_secs_f64()
);

print!(
Expand All @@ -1266,7 +1267,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
CHECKMARK,
diff_cleanup,
total_cleanup,
timing_cleanup_elapsed.as_secs_f64()
default_timing.unwrap_or(timing_cleanup_elapsed).as_secs_f64()
);

let num_dirty_modules = build_state.modules.values().filter(|m| is_dirty(m)).count() as u64;
Expand All @@ -1293,7 +1294,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
style("[4/7]").bold().dim(),
CHECKMARK,
num_dirty_modules,
timing_ast_elapsed.as_secs_f64()
default_timing.unwrap_or(timing_ast_elapsed).as_secs_f64()
);
println!("{}", &err);
}
Expand All @@ -1304,7 +1305,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
LINE_CLEAR,
style("[4/7]").bold().dim(),
CROSS,
timing_ast_elapsed.as_secs_f64()
default_timing.unwrap_or(timing_ast_elapsed).as_secs_f64()
);
println!("{}", &err);
clean::cleanup_after_build(&build_state);
Expand All @@ -1321,7 +1322,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
LINE_CLEAR,
style("[5/7]").bold().dim(),
CHECKMARK,
timing_deps_elapsed.as_secs_f64()
default_timing.unwrap_or(timing_deps_elapsed).as_secs_f64()
);

let start_compiling = Instant::now();
Expand Down Expand Up @@ -1664,7 +1665,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
style("[6/7]").bold().dim(),
CROSS,
num_compiled_modules,
compile_duration.as_secs_f64()
default_timing.unwrap_or(compile_duration).as_secs_f64()
);
return Err(());
} else {
Expand All @@ -1677,7 +1678,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
style("[6/7]").bold().dim(),
CHECKMARK,
num_compiled_modules,
compile_duration.as_secs_f64()
default_timing.unwrap_or(compile_duration).as_secs_f64()
);
}

Expand All @@ -1687,7 +1688,7 @@ pub fn build(filter: &Option<regex::Regex>, path: &str) -> Result<BuildState, ()
LINE_CLEAR,
style("[7/7]").bold().dim(),
CHECKMARK,
timing_total_elapsed.as_secs_f64()
default_timing.unwrap_or(timing_total_elapsed).as_secs_f64()
);

Ok(build_state)
Expand Down
13 changes: 10 additions & 3 deletions src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ pub fn clean_mjs_files(build_state: &BuildState, project_root: &str) {
.filter_map(|module| match &module.source_type {
SourceType::SourceFile(source_file) => {
let package = build_state.packages.get(&module.package_name).unwrap();
let root_package = build_state
.packages
.get(&build_state.root_config_name)
.expect("Could not find root package");
Some((
std::path::PathBuf::from(helpers::get_package_path(
&project_root,
Expand All @@ -102,7 +106,7 @@ pub fn clean_mjs_files(build_state: &BuildState, project_root: &str) {
.join(source_file.implementation.path.to_string())
.to_string_lossy()
.to_string(),
package
root_package
.bsconfig
.suffix
.to_owned()
Expand Down Expand Up @@ -206,6 +210,10 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH

let ast_file_path = path.to_str().unwrap().to_owned();
let res_file_path = get_res_path_from_ast(&ast_file_path);
let root_package = build_state
.packages
.get(&build_state.root_config_name)
.expect("Could not find root package");
match res_file_path {
Some(res_file_path) => {
let _ = ast_modules.insert(
Expand All @@ -217,7 +225,7 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
entry.metadata().unwrap().modified().unwrap(),
ast_file_path,
package.is_root,
package.bsconfig.suffix.to_owned(),
root_package.bsconfig.suffix.to_owned(),
),
);
let _ = ast_rescript_file_locations.insert(res_file_path);
Expand Down Expand Up @@ -280,7 +288,6 @@ pub fn cleanup_previous_build(build_state: &mut BuildState) -> (usize, usize, AH
&build_state.project_root,
*is_root,
);
println!("Removing mjs file: {}", res_file_location);
remove_mjs_file(
&res_file_location,
&suffix.to_owned().unwrap_or(bsconfig::Suffix::Mjs),
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ struct Args {
/// colour as well
#[arg(short, long)]
after_build: Option<String>,

#[arg(short, long)]
no_timing: Option<bool>,
}

fn main() {
Expand All @@ -60,7 +63,7 @@ fn main() {
match command {
Command::Clean => build::clean(&folder),
Command::Build => {
match build::build(&filter, &folder) {
match build::build(&filter, &folder, args.no_timing.unwrap_or(false)) {
Err(()) => std::process::exit(1),
Ok(_) => {
args.after_build.map(|command| cmd::run(command));
Expand All @@ -69,7 +72,7 @@ fn main() {
};
}
Command::Watch => {
let _initial_build = build::build(&filter, &folder);
let _initial_build = build::build(&filter, &folder, false);
args.after_build.clone().map(|command| cmd::run(command));
watcher::start(&filter, &folder, args.after_build);
}
Expand Down
2 changes: 1 addition & 1 deletion src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async fn async_watch(
let _ = q.pop();
}

let _ = build::build(filter, path);
let _ = build::build(filter, path, false);
after_build.clone().map(|command| cmd::run(command));
}
}
Expand Down
9 changes: 9 additions & 0 deletions testrepo/packages/main/src/ModuleWithInterface.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


var hello = "world";

export {
hello ,
}
/* No side effect */
2 changes: 2 additions & 0 deletions testrepo/packages/main/src/ModuleWithInterface.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type hello = string
let hello = "world"
2 changes: 2 additions & 0 deletions testrepo/packages/main/src/ModuleWithInterface.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type hello
let hello: hello
62 changes: 51 additions & 11 deletions tests/compile.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,77 @@
echo "Test: It should compile"
source "./utils.sh"
cd ../testrepo

if RUST_BACKTRACE=1 ../target/release/rewatch clean .;
bold "Test: It should compile"

if rewatch clean &> /dev/null;
then
echo "✅ - Repo Cleaned"
success "Repo Cleaned"
else
echo "❌ - Error Cleaning Repo"
error "Error Cleaning Repo"
exit 1
fi

if RUST_BACKTRACE=1 ../target/release/rewatch build .;
if rewatch &> /dev/null;
then
echo "✅ - Repo Built"
success "Repo Built"
else
echo "❌ - Error Building Repo"
error "Error Building Repo"
exit 1
fi


if git diff --exit-code ./;
then
echo "✅ - Testrepo has no changes"
success "Testrepo has no changes"
else
echo "❌ - Build has changed"
error "Build has changed"
exit 1
fi

node ./packages/main/src/Main.mjs > ./packages/main/src/output.txt

mv ./packages/main/src/Main.res ./packages/main/src/Main2.res
rewatch build --no-timing=true &> ../tests/snapshots/rename-file.txt
mv ./packages/main/src/Main2.res ./packages/main/src/Main.res
rewatch build &> /dev/null
mv ./packages/main/src/ModuleWithInterface.resi ./packages/main/src/ModuleWithInterface2.resi
rewatch build --no-timing=true &> ../tests/snapshots/rename-interface-file.txt
mv ./packages/main/src/ModuleWithInterface2.resi ./packages/main/src/ModuleWithInterface.resi
rewatch build &> /dev/null
mv ./packages/main/src/ModuleWithInterface.res ./packages/main/src/ModuleWithInterface2.res
rewatch build --no-timing=true &> ../tests/snapshots/rename-file-with-interface.txt
mv ./packages/main/src/ModuleWithInterface2.res ./packages/main/src/ModuleWithInterface.res
rewatch build &> /dev/null

# make sure we don't have changes in the test repo
if git diff --exit-code ./;
then
echo "✅ - Output is correct"
success "Output is correct"
else
echo "❌ - Output is incorrect"
error "Output is incorrect"
exit 1
fi

# make sure there are no new files created by the build
# this could happen because of not cleaning up .mjs files
# after we rename files
new_files=$(git ls-files --others --exclude-standard ./)
if [[ $new_files = "" ]];
then
success "No new files created"
else
error "❌ - New files created"
printf "${new_files}\n"
exit 1
fi

# see if the snapshots have changed
changed_snapshots=$(git ls-files --modified ../tests/snapshots)
if git diff --exit-code ../tests/snapshots &> /dev/null;
then
success "Snapshots are correct"
else
error "Snapshots are incorrect:"
printf "${changed_snapshots}\n"
exit 1
fi
9 changes: 9 additions & 0 deletions tests/snapshots/rename-file-with-interface.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[1/7] 🌴 Building package tree...[1/7] ️✅ Built package tree in 0.00s
[2/7] 🔍 Finding source files...Warning: No implementation file found for interface file (skipping): src/ModuleWithInterface.resi
[2/7] ️✅ Found source files in 0.00s
[3/7] 🧹 Cleaning up previous build...[3/7] ️✅ Cleaned 2/9 0.00s
[4/7] ️✅ Parsed 1 source files in 0.00s

[5/7] ️✅ Collected deps in 0.00s
[6/7] ️✅ Compiled 3 modules in 0.00s
[7/7] ️✅ Finished Compilation in 0.00s
Expand Down
8 changes: 8 additions & 0 deletions tests/snapshots/rename-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[1/7] 🌴 Building package tree...[1/7] ️✅ Built package tree in 0.00s
[2/7] 🔍 Finding source files...[2/7] ️✅ Found source files in 0.00s
[3/7] 🧹 Cleaning up previous build...[3/7] ️✅ Cleaned 1/9 0.00s
[4/7] ️✅ Parsed 1 source files in 0.00s

[5/7] ️✅ Collected deps in 0.00s
[6/7] ️✅ Compiled 3 modules in 0.00s
[7/7] ️✅ Finished Compilation in 0.00s
Expand Down
9 changes: 9 additions & 0 deletions tests/snapshots/rename-interface-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[1/7] 🌴 Building package tree...[1/7] ️✅ Built package tree in 0.00s
[2/7] 🔍 Finding source files...Warning: No implementation file found for interface file (skipping): src/ModuleWithInterface2.resi
[2/7] ️✅ Found source files in 0.00s
[3/7] 🧹 Cleaning up previous build...[3/7] ️✅ Cleaned 1/9 0.00s
[4/7] ️✅ Parsed 1 source files in 0.00s

[5/7] ️✅ Collected deps in 0.00s
[6/7] ️✅ Compiled 3 modules in 0.00s
[7/7] ️✅ Finished Compilation in 0.00s
Expand Down
19 changes: 7 additions & 12 deletions tests/suite-ci.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
#!/bin/bash

# Make sure we are in the right directory
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$parent_path"
cd $(dirname $0)

bold () { echo -e "\033[1m$1\033[0m"; }
overwrite() { echo -e "\r\033[1A\033[0K$@"; }
source ./utils.sh

bold "Check if build exists"
if test -f ../target/release/rewatch;
then
echo "✅ - Build exists"
success "Build exists"
else
echo "❌ - Build does not exist. Exiting..."
error "Build does not exist. Exiting..."
exit 1
fi

bold "Make sure the testrepo is clean"
if git diff --exit-code ../testrepo &> /dev/null;
then
overwrite "✅ - Testrepo has no changes"
success "Testrepo has no changes"
else
overwrite "❌ - Testrepo is not clean to start with"
error "Testrepo is not clean to start with"
exit 1
fi

./compile.sh && ./watch.sh

bold "Running Tests"
./compile.sh
./watch--change-file.sh
9 changes: 1 addition & 8 deletions tests/suite.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# This file is needed for local development. It makes sure we kill all the
# subprocesses that are hanging around after the main tests get killed, as the
# watcher will need to run in the background.

trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT

#!/bin/bash
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
cd "$parent_path"
cd $(dirname $0)
./suite-ci.sh
6 changes: 6 additions & 0 deletions tests/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
overwrite() { echo -e "\r\033[1A\033[0K$@"; }
success() { echo -e "- ✅ \033[32m$1\033[0m"; }
error() { echo -e "- 🛑 \033[31m$1\033[0m"; }
bold() { echo -e "\033[1m$1\033[0m"; }
rewatch() { RUST_BACKTRACE=1 ../target/release/rewatch --no-timing=true $1; }
Loading

0 comments on commit efb0151

Please sign in to comment.