Skip to content

Commit

Permalink
Refactor (#6)
Browse files Browse the repository at this point in the history
* Refactor Dockerfile

* Refactor run.sh error branch

* Refactor run.sh fail branch

* Double new lines in failed tests

* Reintroduce multi-stage builds in docker

* init cairo cache (slight increase in speed)

* Merge RUN commands

* remove module name from test output

---------

Co-authored-by: Nenad <nenad@better.giving>
  • Loading branch information
Nenad Misić and Nenad authored Jul 24, 2024
1 parent 918e5e0 commit 62c77ea
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
30 changes: 18 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@ ARG REPO=alpine
ARG IMAGE=3.18
FROM ${REPO}:${IMAGE} AS builder

# set up Scarb
ARG VERSION=v2.6.5
ARG RELEASE=scarb-${VERSION}-x86_64-unknown-linux-musl

RUN apk add --no-cache curl

RUN mkdir opt/test-runner
RUN mkdir opt/test-runner/bin
WORKDIR /tmp
WORKDIR /opt/test-runner/bin/scarb
ADD https://github.com/software-mansion/scarb/releases/download/${VERSION}/${RELEASE}.tar.gz .
RUN tar -xf ${RELEASE}.tar.gz \
&& rm -rf /tmp/${RELEASE}/doc \
&& mv /tmp/${RELEASE} /opt/test-runner/bin/scarb
RUN tar -xf ${RELEASE}.tar.gz --strip-components=1 \
&& rm -rf ./doc \
./bin/scarb-cairo-language-server \
./bin/scarb-cairo-run \
./bin/scarb-snforge-test-collector

FROM ${REPO}:${IMAGE} AS runner

# install packages required to run the tests
# hadolint ignore=DL3018
# install jq package to format test results
RUN apk add --no-cache jq

COPY --from=builder /opt/test-runner/bin/scarb /opt/test-runner/bin/scarb
ENV PATH=$PATH:/opt/test-runner/bin/scarb/bin

WORKDIR /opt/test-runner
COPY . .
# Initialize a scarb cache

COPY bin/run.sh bin/run.sh

# Initialize a Cairo cache
RUN mkdir -p init-cairo-cache/src
COPY Scarb.toml init-cairo-cache/
RUN echo '// dummy file' > init-cairo-cache/src/lib.cairo \
&& scarb --manifest-path init-cairo-cache/Scarb.toml --release build \
&& rm -rf init-cairo-cache/

ENTRYPOINT ["/opt/test-runner/bin/run.sh"]
4 changes: 4 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "leap"
version = "0.1.0"
edition = "2023_11"
9 changes: 3 additions & 6 deletions bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@ cd "${start_dir}" || exit 1
if [ ${exit_code} -eq 0 ]; then
jq -n '{version: 1, status: "pass"}' >"${results_file}"
else
# Sanitize the output
test_output_inline=$(printf '%s' "$test_output")

# Try to distinguish between failing tests and errors
if echo "$test_output_inline" | grep -q "error:"; then
if echo "$test_output" | grep -q "error:"; then
status="error"
sanitized_test_output=$(echo "$test_output_inline" | sed '/Compiling.*$/d' | sed -n -e '/error: could not compile/q;p' | sed "s@$solution_dir@@g")
sanitized_test_output=$(echo "$test_output" | sed -n "/Compiling.*$/d ; s@$solution_dir@@g ; /error: could not compile/q;p")
else
status="fail"
sanitized_test_output=$(echo "$test_output_inline" | awk '/failures:/{y=1;next}y' | sed -n -e '/Error: test result/q;p' | sed -r 's/ //g')
sanitized_test_output=$(echo "$test_output" | sed -n '1,/failures:/d ; /Error: test result/q;p' | sed 's/[[:space:]]\{3\}.\+:://g ; /./G')
fi

jq -n --arg output "${sanitized_test_output}" --arg status "${status}" '{version: 1, status: $status, message: $output}' >"${results_file}"
Expand Down
2 changes: 1 addition & 1 deletion tests/all-fail/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 1,
"status": "fail",
"message": "leap_leap::leap::year_divisible_by_400_is_leap_year - Panicked with \"assertion failed: `is_leap_year(2000)`.\".\nleap_leap::leap::year_divisible_by_4_and_5_is_still_a_leap_year - Panicked with \"assertion failed: `is_leap_year(1960)`.\".\nleap_leap::leap::year_divisible_by_4_not_divisible_by_100_in_leap_year - Panicked with \"assertion failed: `is_leap_year(1996)`.\".\nleap_leap::leap::year_divisible_by_2_not_divisible_by_4_in_common_year - Panicked with \"assertion failed: `!is_leap_year(1970)`.\".\nleap_leap::leap::year_divisible_by_400_but_not_by_125_is_still_a_leap_year - Panicked with \"assertion failed: `is_leap_year(2400)`.\".\nleap_leap::leap::year_not_divisible_by_4_in_common_year - Panicked with \"assertion failed: `!is_leap_year(2015)`.\".\nleap_leap::leap::year_divisible_by_200_not_divisible_by_400_in_common_year - Panicked with \"assertion failed: `!is_leap_year(1800)`.\".\nleap_leap::leap::year_divisible_by_100_not_divisible_by_400_in_common_year - Panicked with \"assertion failed: `!is_leap_year(2100)`.\".\nleap_leap::leap::year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year - Panicked with \"assertion failed: `!is_leap_year(1900)`.\"."
"message": "year_divisible_by_400_is_leap_year - Panicked with \"assertion failed: `is_leap_year(2000)`.\".\n\nyear_divisible_by_4_and_5_is_still_a_leap_year - Panicked with \"assertion failed: `is_leap_year(1960)`.\".\n\nyear_divisible_by_4_not_divisible_by_100_in_leap_year - Panicked with \"assertion failed: `is_leap_year(1996)`.\".\n\nyear_divisible_by_2_not_divisible_by_4_in_common_year - Panicked with \"assertion failed: `!is_leap_year(1970)`.\".\n\nyear_divisible_by_400_but_not_by_125_is_still_a_leap_year - Panicked with \"assertion failed: `is_leap_year(2400)`.\".\n\nyear_not_divisible_by_4_in_common_year - Panicked with \"assertion failed: `!is_leap_year(2015)`.\".\n\nyear_divisible_by_200_not_divisible_by_400_in_common_year - Panicked with \"assertion failed: `!is_leap_year(1800)`.\".\n\nyear_divisible_by_100_not_divisible_by_400_in_common_year - Panicked with \"assertion failed: `!is_leap_year(2100)`.\".\n\nyear_divisible_by_100_but_not_by_3_is_still_not_a_leap_year - Panicked with \"assertion failed: `!is_leap_year(1900)`.\"."
}
2 changes: 1 addition & 1 deletion tests/partial-fail/expected_results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": 1,
"status": "fail",
"message": "leap_leap::leap::year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year - Panicked with \"assertion failed: `!is_leap_year(1900)`.\".\nleap_leap::leap::year_divisible_by_100_not_divisible_by_400_in_common_year - Panicked with \"assertion failed: `!is_leap_year(2100)`.\".\nleap_leap::leap::year_divisible_by_200_not_divisible_by_400_in_common_year - Panicked with \"assertion failed: `!is_leap_year(1800)`.\"."
"message": "year_divisible_by_100_but_not_by_3_is_still_not_a_leap_year - Panicked with \"assertion failed: `!is_leap_year(1900)`.\".\n\nyear_divisible_by_100_not_divisible_by_400_in_common_year - Panicked with \"assertion failed: `!is_leap_year(2100)`.\".\n\nyear_divisible_by_200_not_divisible_by_400_in_common_year - Panicked with \"assertion failed: `!is_leap_year(1800)`.\"."
}

0 comments on commit 62c77ea

Please sign in to comment.