Skip to content

Commit 567b5b8

Browse files
authored
One justfile to run them all (#7369)
## Description This PR brings all the scripts that we have, and push a couple more into one `just` file (https://github.com/casey/just). Initially we are just calling the script directly, but we can improve it later. ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
1 parent c58ddbc commit 567b5b8

File tree

8 files changed

+335
-1
lines changed

8 files changed

+335
-1
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,35 @@ Confirm the Sway toolchain built successfully:
5050
cargo run --bin forc -- --help
5151
```
5252

53+
## All other scripts/commands
54+
55+
For all other scripts and commands use https://github.com/casey/just:
56+
57+
```
58+
> just --list
59+
Available recipes:
60+
[automation]
61+
update-contract-ids
62+
update-fuel-dependencies
63+
64+
[benchmark]
65+
benchmark
66+
benchmark-tests
67+
collect-gas-usage
68+
69+
[build]
70+
build-highlightjs
71+
build-prism
72+
generate-sway-lib-std
73+
74+
[ci]
75+
ci-check
76+
install-ci-check
77+
78+
[test]
79+
test-forc-fmt-check-panic
80+
```
81+
5382
## Contributing to Sway
5483

5584
We welcome contributions to Sway!

justfile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
[group('ci')]
2+
[confirm("Do you want to install cargo-sort, cargo-generate and cargo-udeps from crates.io?")]
3+
install-ci-check:
4+
cargo install cargo-sort
5+
cargo install cargo-generate
6+
cargo install cargo-udeps
7+
8+
[group('ci')]
9+
ci-check:
10+
bash ./ci_checks.sh
11+
12+
[group('automation')]
13+
[confirm("Do you want to bump all fuel maintained dependencies?")]
14+
update-fuel-dependencies:
15+
bash ./update_fuel_dependencies.sh
16+
17+
[group('automation')]
18+
[confirm("Do you want to automatically update contractIds in this repo?")]
19+
update-contract-ids:
20+
bash ./test/update-contract-ids.sh
21+
22+
[group('benchmark')]
23+
benchmark:
24+
bash ./benchmark.sh
25+
26+
[group('benchmark')]
27+
benchmark-tests:
28+
bash ./test/bench.sh
29+
30+
[group('benchmark')]
31+
collect-gas-usage:
32+
cargo r -p test --release -- --verbose --forc-test-only | ./scripts/compare-gas-usage/extract-gas-usage.sh
33+
34+
[group('build')]
35+
build-prism:
36+
cd ./scripts/prism && ./build.sh
37+
38+
[group('build')]
39+
build-highlightjs:
40+
cd ./scripts/highlightjs && ./build.sh
41+
42+
[group('build')]
43+
generate-sway-lib-std:
44+
cd ./sway-lib-std && ./generate.sh
45+
46+
[group('test')]
47+
test-forc-fmt-check-panic:
48+
cd ./scripts/formatter && ./forc-fmt-check-panic.sh

scripts/bisect-forc/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
`bisect-forc.sh` will automatically run `forc bisect` searching for a different behaviour between two commits.
2+
3+
The script accepts three arguments:
4+
5+
```
6+
bisect-forc.sh sway_project test 30s
7+
```
8+
1 - First argument is the sway project that will be compiled over and over until a different behavior is found;
9+
2 - The second argument is which forc subcommand will be used. It defaults to `build`.
10+
11+
So, `forc` will be run as:
12+
13+
```
14+
> forc <SECONDARGUMENT> --path <FIRSTARGUMENT>
15+
```
16+
17+
3 - The third argument is a sleep that is taken between compilations to avoid notebooks enter into thermal throttle mode, or that weaker machines become unusable. Default to zero.

scripts/bisect-forc/bisect-forc.sh

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#! /bin/bash
2+
3+
PROJ=$1
4+
SLEEP_BETWEEN=$3
5+
CACHE="$HOME/.cache/sway-bench"
6+
7+
NC='\033[0m'
8+
BOLD_GREEN="\033[1;32m"
9+
BOLD_RED='\033[1;31m'
10+
BOLD_WHITE='\033[1;97m'
11+
12+
# $1 = commit hash
13+
compile_and_cp_to_cache() {
14+
if [ ! -f "$CACHE/$1" ]; then
15+
if [[ -n $SLEEP_BETWEEN ]]; then
16+
sleep "$SLEEP_BETWEEN"
17+
fi
18+
cargo b --release &>> /dev/null
19+
cp target/release/forc "$CACHE/$1" &>> /dev/null
20+
fi
21+
}
22+
23+
run_cargo() {
24+
if [ "$2" = "" ]; then
25+
bash -c "$CACHE/$CACHENAME build --path $PROJ" &>> /dev/null
26+
echo "$?"
27+
else
28+
bash -c "$CACHE/$CACHENAME $2 --path $PROJ" &>> /dev/null
29+
echo "$?"
30+
fi
31+
}
32+
33+
INITIAL_COMMIT="$(git show -s --format='%H' HEAD)"
34+
END_COMMIT=""
35+
36+
echo "Forc command will be:"
37+
if [ "$2" = "" ]; then
38+
echo "> forc build --path $PROJ"
39+
else
40+
echo "> forc $2 --path $PROJ"
41+
fi
42+
43+
echo "Starting the search at: "
44+
echo -n " "
45+
git log -1 --oneline
46+
47+
echo -n "Running: "
48+
49+
CACHENAME="$(git show -s --format='%as-%ct-%H' HEAD)"
50+
compile_and_cp_to_cache "$CACHENAME"
51+
INITIAL_COMPILATION_STATUS=$(run_cargo)
52+
53+
if [ "$INITIAL_COMPILATION_STATUS" = "0" ]; then
54+
echo -e " ${BOLD_GREEN}Ok${NC}"
55+
echo ""
56+
echo "Searching the newest version which compilation was failing."
57+
else
58+
echo -e " ${BOLD_RED}Failed${NC}"
59+
echo ""
60+
echo "Searching the newest version which compilation was succeeding."
61+
fi
62+
63+
64+
for HASH in `git log --format="%H" --tags --no-walk`; do
65+
git checkout "$HASH" &>> /dev/null
66+
git checkout . &>> /dev/null
67+
68+
git log -1 --oneline
69+
70+
CACHENAME="$(git show -s --format='%as-%ct-%H' HEAD)"
71+
compile_and_cp_to_cache "$CACHENAME"
72+
LAST_STATUS=$(run_cargo)
73+
if [ "$INITIAL_COMPILATION_STATUS" != "$LAST_STATUS" ]; then
74+
echo -e "^^^^^^^^^ ${BOLD_WHITE}This version result is different!${NC}"
75+
break
76+
fi
77+
done
78+
79+
END_COMMIT="$(git show -s --format='%H' HEAD)"
80+
81+
echo ""
82+
echo -e "${BOLD_WHITE}Starting bisect between: ${NC}$INITIAL_COMMIT..$END_COMMIT"
83+
84+
git checkout $INITIAL_COMMIT &>> /dev/null
85+
86+
git bisect start &>> /dev/null
87+
git bisect new $INITIAL_COMMIT &>> /dev/null
88+
git bisect old $END_COMMIT &>> /dev/null
89+
90+
while :
91+
do
92+
#echo "-----------------------"
93+
#git --no-pager bisect visualize --oneline
94+
95+
git bisect next | grep "is the first new commit" &>> /dev/null
96+
if [ "$LAST_STATUS" = "0" ]; then
97+
FIRST_COMMIT="$(git bisect next 2>&1 | head -n 1 | cut -f1 -d" ")"
98+
git checkout "$FIRST_COMMIT" &>> /dev/null
99+
break
100+
fi
101+
102+
git bisect next | grep "Bisecting"
103+
if [ "$LAST_STATUS" != "0" ]; then
104+
break
105+
fi
106+
107+
git checkout . &>> /dev/null
108+
109+
CACHENAME="$(git show -s --format='%as-%ct-%H' HEAD)"
110+
compile_and_cp_to_cache "$CACHENAME"
111+
LAST_STATUS=$(run_cargo)
112+
if [ "$LAST_STATUS" = "$INITIAL_COMPILATION_STATUS" ]; then
113+
git bisect new &>> /dev/null
114+
else
115+
git bisect old &>> /dev/null
116+
fi
117+
done
118+
119+
FOUND_COMMIT="$(git show -s --format='%H' HEAD)"
120+
121+
echo -e "${BOLD_GREEN}Found!${NC} - ${FOUND_COMMIT}"
122+
echo ""
123+
124+
125+
# check this commit has the same behaviour
126+
echo -n "checking the found commit has the same behaviour as the initial commit..."
127+
128+
CACHENAME="$(git show -s --format='%as-%ct-%H' HEAD)"
129+
compile_and_cp_to_cache "$CACHENAME"
130+
LAST_STATUS=$(run_cargo)
131+
132+
if [ "$INITIAL_COMPILATION_STATUS" != "$LAST_STATUS" ]; then
133+
echo -e " ${BOLD_RED}Unexpected exit code${NC}"
134+
exit 1
135+
fi
136+
137+
echo -e " ${BOLD_GREEN}Ok${NC}"
138+
139+
## check the previous commit has the inverse
140+
echo -n "checking the previous commit has the inverse behaviour as the initial commit..."
141+
142+
git checkout HEAD~1 &>> /dev/null
143+
PREVIOUS_COMMIT="$(git show -s --format='%H' HEAD)"
144+
CACHENAME="$(git show -s --format='%as-%ct-%H' HEAD)"
145+
compile_and_cp_to_cache "$CACHENAME"
146+
LAST_STATUS=$(run_cargo)
147+
148+
if [ "$INITIAL_COMPILATION_STATUS" = "$LAST_STATUS" ]; then
149+
echo -e " ${BOLD_RED}Unexpected exit code${NC}"
150+
exit 1
151+
fi
152+
153+
echo -e " ${BOLD_GREEN}Ok${NC}"
154+
155+
echo ""
156+
157+
git checkout . &>> /dev/null
158+
git bisect reset &>> /dev/null
159+
160+
git checkout "$FOUND_COMMIT" &>> /dev/null
161+
echo "This is the commit that changed the compiler behavior"
162+
git log -1 --oneline
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# This script extracts full test names and test gas usage from a `forc test` output.
4+
# Usage: `forc test | test_gas_usage.sh`.
5+
6+
current_suite=""
7+
results=()
8+
9+
while IFS= read -r line; do
10+
# printf 'Line: %s\n' "$line"
11+
12+
if [[ $line =~ ^tested\ --\ ([^[:space:]]+) ]]; then
13+
current_suite="${BASH_REMATCH[1]}"
14+
fi
15+
# printf 'Suite: %s\n' "$current_suite"
16+
17+
if [[ $line =~ ^[[:space:]]*test[[:space:]]([^\ ]+)[[:space:]]\.\.\.[[:space:]].*,[[:space:]]([0-9]+)[[:space:]]gas\) ]]; then
18+
test_name="${BASH_REMATCH[1]}"
19+
# printf 'Test: %s\n' "$test_name"
20+
gas="${BASH_REMATCH[2]}"
21+
# printf 'Gas: %s\n' "$gas"
22+
results+=("${current_suite}::${test_name},${gas}")
23+
fi
24+
done
25+
26+
printf '%s\n' "${results[@]}" | sort
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
# This script compares test gas usage from two outputs of the `test_gas_usage.sh` script.
4+
# The result of the comparison can be printed either as a Markdown table or a CSV file.
5+
# Usage: `compare_test_gas_usage.sh <before>.csv <after>.csv [MD|CSV]`.
6+
7+
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
8+
echo "Usage: $0 <before>.csv <after>.csv [MD|CSV]"
9+
exit 1
10+
fi
11+
12+
before_file="$1"
13+
after_file="$2"
14+
output_format="${3:-MD}"
15+
16+
if [ "$output_format" == "CSV" ]; then
17+
echo "Test,Before,After,Percentage"
18+
else
19+
echo "| Test | Before | After | Percentage |"
20+
echo "|------|--------|-------|------------|"
21+
fi
22+
23+
paste -d, "$before_file" "$after_file" | while IFS=',' read -r test1 before test2 after; do
24+
if [ "$before" != "$after" ]; then
25+
diff=$((before - after))
26+
percent=$(LC_NUMERIC=C awk -v d="$diff" -v b="$before" 'BEGIN { printf "%.2f", (d / b) * 100 }')
27+
28+
if [ "$output_format" == "CSV" ]; then
29+
echo "$test1,$before,$after,$percent"
30+
else
31+
echo "| $test1 | $before | $after | $percent% |"
32+
fi
33+
fi
34+
done

test/src/e2e_vm_tests/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,18 @@ impl TestContext {
669669
result.map(|tested_pkgs| {
670670
let mut failed = vec![];
671671
for pkg in tested_pkgs {
672+
if !pkg.tests.is_empty() {
673+
println!();
674+
}
672675
for test in pkg.tests.into_iter() {
673676
if verbose {
674-
println!("Test: {} {}", test.name, test.passed());
677+
//"test incorrect_def_modeling ... ok (17.673µs, 59 gas)"
678+
println!(" test {} ... {} ({:?}, {} gas)",
679+
test.name,
680+
if test.passed() { "ok" } else { "nok" },
681+
test.duration,
682+
test.gas_used,
683+
);
675684
for log in test.logs.iter() {
676685
println!("{log:?}");
677686
}
@@ -782,6 +791,9 @@ pub async fn run(filter_config: &FilterConfig, run_config: &RunConfig) -> Result
782791
if filter_config.contract_only {
783792
tests.retain(|t| t.category == TestCategory::RunsWithContract);
784793
}
794+
if filter_config.forc_test_only {
795+
tests.retain(|t| t.category == TestCategory::UnitTestsPass);
796+
}
785797
if filter_config.first_only && !tests.is_empty() {
786798
tests = vec![tests.remove(0)];
787799
}

test/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ struct Cli {
3838
#[arg(long, visible_alias = "contract")]
3939
contract_only: bool,
4040

41+
/// Only run tests that run "forc test"
42+
#[arg(long, visible_alias = "forc-test")]
43+
forc_test_only: bool,
44+
4145
/// Only run the first test
4246
#[arg(long, visible_alias = "first")]
4347
first_only: bool,
@@ -136,6 +140,7 @@ pub struct FilterConfig {
136140
pub exclude_std: bool,
137141
pub contract_only: bool,
138142
pub first_only: bool,
143+
pub forc_test_only: bool,
139144
}
140145

141146
#[derive(Debug, Clone)]
@@ -165,6 +170,7 @@ async fn main() -> Result<()> {
165170
abi_only: cli.abi_only,
166171
exclude_std: cli.exclude_std,
167172
contract_only: cli.contract_only,
173+
forc_test_only: cli.forc_test_only,
168174
first_only: cli.first_only,
169175
};
170176
let build_target = match cli.build_target {

0 commit comments

Comments
 (0)