Skip to content

Commit

Permalink
Add smoke tests (#22)
Browse files Browse the repository at this point in the history
* Ignore more files in Docker context

* Add scripts to run tests

* Run smoke tests in CI

* Add smoke tests
  • Loading branch information
ErikSchierboom committed Oct 31, 2023
1 parent ab66d87 commit 062f515
Show file tree
Hide file tree
Showing 13 changed files with 1,691 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ node_modules
.gitattributes
.dockerignore
Dockerfile
test/
bin/
!bin/run.sh
26 changes: 25 additions & 1 deletion .github/workflows/ci.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Lint code
run: yarn lint

ci:
unit_tests:
runs-on: ubuntu-latest

strategy:
Expand All @@ -40,3 +40,27 @@ jobs:

- name: Install project dependencies and run tests
run: yarn install --frozen-lockfile

smoke_tests:
runs-on: ubuntu-22.04
steps:
- name: Checkout code
uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@4c0219f9ac95b02789c1075625400b2acbff50b1
with:
install: true

- name: Build Docker image and store in cache
uses: docker/build-push-action@2eb1c1961a95fc15694676618e422e8ba1d63825
with:
context: .
push: false
load: true
tags: exercism/typescript-representer
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Run Tests in Docker
run: bin/run-tests-in-docker.sh
43 changes: 43 additions & 0 deletions bin/run-in-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env sh

# Synopsis:
# Run the representer on a solution using the representer Docker image.
# The representer Docker image is built automatically.

# Arguments:
# $1: exercise slug
# $2: absolute path to solution folder
# $3: absolute path to output directory

# Output:
# Writes the test results to a results.json file in the passed-in output directory.
# The test results are formatted according to the specifications at https://github.com/exercism/docs/blob/main/building/tooling/representers/interface.md

# Example:
# ./bin/run-in-docker.sh two-fer /absolute/path/to/two-fer/solution/folder/ /absolute/path/to/output/directory/

# If any required arguments is missing, print the usage and exit
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
echo "usage: ./bin/run-in-docker.sh exercise-slug /absolute/path/to/solution/folder/ /absolute/path/to/output/directory/"
exit 1
fi

slug="$1"
input_dir="${2%/}"
output_dir="${3%/}"

# Create the output directory if it doesn't exist
mkdir -p "${output_dir}"

# Build the Docker image
docker build --rm -t exercism/typescript-representer .

# Run the Docker image using the settings mimicking the production environment
docker run \
--rm \
--network none \
--read-only \
--mount type=bind,source="${input_dir}",destination=/solution \
--mount type=bind,source="${output_dir}",destination=/output \
--mount type=tmpfs,destination=/tmp \
exercism/typescript-representer "${slug}" /solution /output
28 changes: 28 additions & 0 deletions bin/run-tests-in-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env sh

# Synopsis:
# Test the representer Docker image by running it against a predefined set of
# solutions with an expected output.
# The representer Docker image is built automatically.

# Output:
# Outputs the diff of the expected representation and mapping against the
# actual representation and mapping generated by the representer.

# Example:
# ./bin/run-tests-in-docker.sh

# Build the Docker image
docker build --rm -t exercism/typescript-representer .

# Run the Docker image using the settings mimicking the production environment
docker run \
--rm \
--network none \
--read-only \
--mount type=bind,source="${PWD}/test",destination=/opt/representer/test \
--mount type=tmpfs,destination=/tmp \
--volume "${PWD}/bin/run-tests.sh:/opt/representer/bin/run-tests.sh" \
--workdir /opt/representer \
--entrypoint /opt/representer/bin/run-tests.sh \
exercism/typescript-representer
44 changes: 44 additions & 0 deletions bin/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env sh

# Synopsis:
# Test the representer by running it against a predefined set of solutions
# with an expected output.

# Output:
# Outputs the diff of the expected representation and mapping against the
# actual representation and mapping generated by the representer.

# Example:
# ./bin/run-tests.sh

exit_code=0

# We need to copy the fixtures to a temp directory as the user
# running within the Docker container does not have permissions
# to run the sed command on the fixtures directory
fixtures_dir="test/fixtures"
tmp_fixtures_dir="/tmp/test/fixtures"
rm -rf "${tmp_fixtures_dir}"
mkdir -p "${tmp_fixtures_dir}"
cp -R ${fixtures_dir}/* "${tmp_fixtures_dir}"

# Iterate over all test directories
for test_file in $(find "${tmp_fixtures_dir}" -name expected_mapping.json); do
slug=$(echo "${test_file:${#tmp_fixtures_dir}+1}" | cut -d / -f 1)
test_dir=$(dirname "${test_file}")
test_dir_name=$(basename "${test_dir}")
test_dir_path=$(realpath "${test_dir}")

bin/run.sh "${slug}" "${test_dir_path}" "${test_dir_path}"

for file in representation.txt mapping.json; do
expected_file="expected_${file}"
echo "${test_dir_name}: comparing ${file} to ${expected_file}"

if ! diff "${test_dir_path}/${file}" "${test_dir_path}/${expected_file}"; then
exit_code=1
fi
done
done

exit ${exit_code}
28 changes: 28 additions & 0 deletions test/fixtures/clock/pass/expected_mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"IDENTIFIER_0": "Clock",
"IDENTIFIER_1": "hour",
"IDENTIFIER_2": "minute",
"IDENTIFIER_3": "reset",
"IDENTIFIER_4": "totalMinutes",
"IDENTIFIER_5": "adjustTime",
"IDENTIFIER_6": "getHour",
"IDENTIFIER_7": "getMinute",
"IDENTIFIER_8": "formatNumber",
"IDENTIFIER_9": "numberToFormat",
"IDENTIFIER_10": "numberString",
"IDENTIFIER_11": "length",
"IDENTIFIER_12": "plus",
"IDENTIFIER_13": "minutes",
"IDENTIFIER_14": "minus",
"IDENTIFIER_15": "equals",
"IDENTIFIER_16": "clock",
"IDENTIFIER_17": "delta",
"IDENTIFIER_18": "minutesPerDay",
"IDENTIFIER_19": "minutesPerHour",
"IDENTIFIER_20": "hoursPerDay",
"IDENTIFIER_21": "Math",
"IDENTIFIER_22": "abs",
"IDENTIFIER_23": "currentMinutes",
"IDENTIFIER_24": "newMinutes",
"IDENTIFIER_25": "floor"
}

0 comments on commit 062f515

Please sign in to comment.