From 9af6c195709dab9a252c73fe26d29341f89b3d38 Mon Sep 17 00:00:00 2001 From: Jaap Frolich Date: Mon, 24 Jul 2023 17:14:23 +0200 Subject: [PATCH] :test_tube: - Add tests and improve test suite --- src/build.rs | 21 ++++--- src/clean.rs | 13 +++- src/main.rs | 7 ++- src/watcher.rs | 2 +- .../packages/main/src/ModuleWithInterface.mjs | 9 +++ .../packages/main/src/ModuleWithInterface.res | 2 + .../main/src/ModuleWithInterface.resi | 2 + tests/compile.sh | 62 +++++++++++++++---- .../snapshots/rename-file-with-interface.txt | 9 +++ tests/snapshots/rename-file.txt | 8 +++ tests/snapshots/rename-interface-file.txt | 9 +++ tests/suite-ci.sh | 19 +++--- tests/suite.sh | 9 +-- tests/utils.sh | 6 ++ tests/watch--change-file.sh | 44 ------------- tests/watch.sh | 59 ++++++++++++++++++ 16 files changed, 190 insertions(+), 91 deletions(-) create mode 100644 testrepo/packages/main/src/ModuleWithInterface.mjs create mode 100644 testrepo/packages/main/src/ModuleWithInterface.res create mode 100644 testrepo/packages/main/src/ModuleWithInterface.resi create mode 100644 tests/snapshots/rename-file-with-interface.txt create mode 100644 tests/snapshots/rename-file.txt create mode 100644 tests/snapshots/rename-interface-file.txt create mode 100644 tests/utils.sh delete mode 100755 tests/watch--change-file.sh create mode 100755 tests/watch.sh diff --git a/src/build.rs b/src/build.rs index 8dd2b6a..66ea81f 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1207,11 +1207,12 @@ fn compute_file_hash(path: &str) -> Option { } } -pub fn build(filter: &Option, path: &str) -> Result { +pub fn build(filter: &Option, path: &str, no_timing: bool) -> Result { 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 = if no_timing { Some(std::time::Duration::new(0.0 as u64, 0.0 as u32)) } else { None }; print!( "{} {} Building package tree...", @@ -1229,7 +1230,7 @@ pub fn build(filter: &Option, path: &str) -> Result, path: &str) -> Result, path: &str) -> Result, path: &str) -> Result, path: &str) -> Result, path: &str) -> Result, path: &str) -> Result, path: &str) -> Result, path: &str) -> Result { 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, @@ -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() @@ -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( @@ -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); @@ -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), diff --git a/src/main.rs b/src/main.rs index 2ccbcd5..b0e8e0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,6 +45,9 @@ struct Args { /// colour as well #[arg(short, long)] after_build: Option, + + #[arg(short, long)] + no_timing: Option, } fn main() { @@ -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)); @@ -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); } diff --git a/src/watcher.rs b/src/watcher.rs index 7a2cbe6..a96ccf9 100644 --- a/src/watcher.rs +++ b/src/watcher.rs @@ -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)); } } diff --git a/testrepo/packages/main/src/ModuleWithInterface.mjs b/testrepo/packages/main/src/ModuleWithInterface.mjs new file mode 100644 index 0000000..d60faa7 --- /dev/null +++ b/testrepo/packages/main/src/ModuleWithInterface.mjs @@ -0,0 +1,9 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +var hello = "world"; + +export { + hello , +} +/* No side effect */ diff --git a/testrepo/packages/main/src/ModuleWithInterface.res b/testrepo/packages/main/src/ModuleWithInterface.res new file mode 100644 index 0000000..1ca4d44 --- /dev/null +++ b/testrepo/packages/main/src/ModuleWithInterface.res @@ -0,0 +1,2 @@ +type hello = string +let hello = "world" diff --git a/testrepo/packages/main/src/ModuleWithInterface.resi b/testrepo/packages/main/src/ModuleWithInterface.resi new file mode 100644 index 0000000..42a1f84 --- /dev/null +++ b/testrepo/packages/main/src/ModuleWithInterface.resi @@ -0,0 +1,2 @@ +type hello +let hello: hello diff --git a/tests/compile.sh b/tests/compile.sh index 05a1245..d0f3ff6 100755 --- a/tests/compile.sh +++ b/tests/compile.sh @@ -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 \ No newline at end of file diff --git a/tests/snapshots/rename-file-with-interface.txt b/tests/snapshots/rename-file-with-interface.txt new file mode 100644 index 0000000..0f4664a --- /dev/null +++ b/tests/snapshots/rename-file-with-interface.txt @@ -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 diff --git a/tests/snapshots/rename-file.txt b/tests/snapshots/rename-file.txt new file mode 100644 index 0000000..a9d55d1 --- /dev/null +++ b/tests/snapshots/rename-file.txt @@ -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 diff --git a/tests/snapshots/rename-interface-file.txt b/tests/snapshots/rename-interface-file.txt new file mode 100644 index 0000000..5ecc407 --- /dev/null +++ b/tests/snapshots/rename-interface-file.txt @@ -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 diff --git a/tests/suite-ci.sh b/tests/suite-ci.sh index 24e6cf0..740c11d 100755 --- a/tests/suite-ci.sh +++ b/tests/suite-ci.sh @@ -1,31 +1,26 @@ #!/bin/bash +source ./utils.sh # Make sure we are in the right directory -parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P ) -cd "$parent_path" - -bold () { echo -e "\033[1m$1\033[0m"; } -overwrite() { echo -e "\r\033[1A\033[0K$@"; } +cd $(dirname $0) 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 diff --git a/tests/suite.sh b/tests/suite.sh index 2bdb614..8241a09 100755 --- a/tests/suite.sh +++ b/tests/suite.sh @@ -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 diff --git a/tests/utils.sh b/tests/utils.sh new file mode 100644 index 0000000..d4a8957 --- /dev/null +++ b/tests/utils.sh @@ -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; } \ No newline at end of file diff --git a/tests/watch--change-file.sh b/tests/watch--change-file.sh deleted file mode 100755 index 292772d..0000000 --- a/tests/watch--change-file.sh +++ /dev/null @@ -1,44 +0,0 @@ -echo "Test: It should watch" -cd ../testrepo - -if RUST_BACKTRACE=1 ../target/release/rewatch clean . &> /dev/null; -then - echo "✅ - Repo Cleaned" -else - echo "❌ - Error Cleaning Repo" - exit 1 -fi - -RUST_BACKTRACE=1 ../target/release/rewatch watch . &>/dev/null & -echo "✅ - Watcher Started" - -echo 'Js.log("added-by-test")' >> ./packages/main/src/Main.res - -sleep 1 - -if node ./packages/main/src/Main.mjs | grep 'added-by-test' &> /dev/null; -then - echo "✅ - Output is correct" -else - echo "❌ - Output is incorrect" - exit 1 -fi - -sleep 1 - -if [[ $OSTYPE == 'darwin'* ]]; -then - sed -i '' '/Js.log("added-by-test")/d' ./packages/main/src/Main.res; -else - sed -i '/Js.log("added-by-test")/d' ./packages/main/src/Main.res; -fi - -sleep 1 - -if git diff --exit-code ./ -then - echo "✅ - Adding and removing changes nothing" -else - echo "❌ - Adding and removing changes left some artifacts" - exit 1 -fi diff --git a/tests/watch.sh b/tests/watch.sh new file mode 100755 index 0000000..338135b --- /dev/null +++ b/tests/watch.sh @@ -0,0 +1,59 @@ +source "./utils.sh" +cd ../testrepo + +bold "Test: It should watch" + +replace() { + if [[ $OSTYPE == 'darwin'* ]]; + then + sed -i '' $1 $2; + else + sed -i $1 $2; + fi +} + +if rewatch clean &> /dev/null; +then + success "Repo Cleaned" +else + error "Error Cleaning Repo" + exit 1 +fi + +exit_watcher() { + # we need to kill the parent process (rewatch) + kill $(pgrep -P $!); +} + +rewatch watch &>/dev/null & +success "Watcher Started" + +echo 'Js.log("added-by-test")' >> ./packages/main/src/Main.res + +sleep 1 + +if node ./packages/main/src/Main.mjs | grep 'added-by-test' &> /dev/null; +then + success "Output is correct" +else + error "Output is incorrect" + exit_watcher + exit 1 +fi + +sleep 1 + +replace '/Js.log("added-by-test")/d' ./packages/main/src/Main.res; + +sleep 1 + +if git diff --exit-code ./ +then + success "Adding and removing changes nothing" +else + error "Adding and removing changes left some artifacts" + exit_watcher + exit 1 +fi + +exit_watcher \ No newline at end of file