Skip to content

Commit

Permalink
Add smoke tests (#143)
Browse files Browse the repository at this point in the history
* Remove integration tests project

* Rename test files

* Convert to tests dir

* Remove scripts

* Add root bin dir
  • Loading branch information
ErikSchierboom authored Aug 13, 2023
1 parent 30e7c1e commit 247f29b
Show file tree
Hide file tree
Showing 83 changed files with 185 additions and 297 deletions.
15 changes: 4 additions & 11 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
*
!run.sh
!src/Exercism.Representers.FSharp
src/Exercism.Representers.FSharp/bin
src/Exercism.Representers.FSharp/obj
.appends
.git
.github
.gitignore
.gitattributes
.dockerignore
Dockerfile
!/bin/run.sh
!/src/Exercism.Representers.FSharp
/src/Exercism.Representers.FSharp/bin
/src/Exercism.Representers.FSharp/obj
28 changes: 22 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,27 @@ on:
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-18.04
build:
name: Tests
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b
- uses: actions/[email protected]
- name: Checkout code
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9

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

- name: Build and push
uses: docker/build-push-action@2eb1c1961a95fc15694676618e422e8ba1d63825
with:
dotnet-version: "3.1.300"
- run: pwsh ./test.ps1
context: .
push: false
load: true
tags: exercism/fsharp-representer
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Run Tests in Docker
run: bin/run-tests-in-docker.sh
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
.dotnet
*.DotSettings.user
representation.txt
representation.json
mapping.json
bin/
obj/
!/bin
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ WORKDIR /opt/representer
COPY --from=build /opt/representer/ .
COPY --from=build /usr/local/bin/ /usr/local/bin/

COPY run.sh /opt/representer/bin/
COPY bin/run.sh /opt/representer/bin/

ENTRYPOINT ["sh", "/opt/representer/bin/run.sh"]
8 changes: 1 addition & 7 deletions Exercism.Representers.FSharp.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 12.00
#
#
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Exercism.Representers.FSharp", "src\Exercism.Representers.FSharp\Exercism.Representers.FSharp.fsproj", "{9428D8F1-00A3-4246-85DE-F0D31978D853}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Exercism.Representers.FSharp.IntegrationTests", "test\Exercism.Representers.FSharp.IntegrationTests\Exercism.Representers.FSharp.IntegrationTests.fsproj", "{3AAC4D42-D417-4CB1-A82B-82EC8F73330E}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Exercism.Representers.FSharp.Bulk", "src\Exercism.Representers.FSharp.Bulk\Exercism.Representers.FSharp.Bulk.fsproj", "{93AB5F81-CE6D-47A0-BAB8-8FF66D1F53E2}"
EndProject
Global
Expand All @@ -17,10 +15,6 @@ Global
{9428D8F1-00A3-4246-85DE-F0D31978D853}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9428D8F1-00A3-4246-85DE-F0D31978D853}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9428D8F1-00A3-4246-85DE-F0D31978D853}.Release|Any CPU.Build.0 = Release|Any CPU
{3AAC4D42-D417-4CB1-A82B-82EC8F73330E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3AAC4D42-D417-4CB1-A82B-82EC8F73330E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3AAC4D42-D417-4CB1-A82B-82EC8F73330E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3AAC4D42-D417-4CB1-A82B-82EC8F73330E}.Release|Any CPU.Build.0 = Release|Any CPU
{93AB5F81-CE6D-47A0-BAB8-8FF66D1F53E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{93AB5F81-CE6D-47A0-BAB8-8FF66D1F53E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{93AB5F81-CE6D-47A0-BAB8-8FF66D1F53E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
22 changes: 2 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,19 @@ This repository contains the F# representer, which implements the [representer i
To create a representation of a solution, follow these steps:

1. Open a command prompt in the root directory.
1. Run `./generate.ps1 <exercise> <input-directory> <output-directory>`. This script will generate a representation for the solution found in `<input-directory>`.
1. Run `./bin/run.sh <exercise> <input-directory> <output-directory>`. This script will generate a representation for the solution found in `<input-directory>`.
1. Once the script has completed, the representation will be written to `<output-directory>/representation.txt`.

## Generate a representation for multiple solutions

To create representations for multiple solutions at once, follow these steps:

1. Open a command prompt in the root directory.
1. Run `./generate-in-bulk.ps1 <exercise> <input-directory>`. This script will create a representation for the solution in each directory sub-directory of `<input-directory>`.
1. Once the script has completed, a representation for the solutions will be written in each directory.

## Generate a representation for a solution using Docker

To generate a representation for a solution using a Docker container, follow these steps:

1. Open a command prompt in the root directory.
1. Run `./generate-in-docker.ps1 <exercise> <input-directory> <output-directory>`. This script will:
1. Run `./bin/run-in-docker.sh <exercise> <input-directory> <output-directory>`. This script will:
1. Build the representer Docker image (if necessary).
1. Run the representer Docker image (as a container), passing the specified `exercise`, `input-directory` and `output-directory` arguments.
1. Once the script has completed, the representation can be found at `<output-directory>/representation.txt`.

## Source code formatting

This repository uses the [fantomas][fantomas] and [prettier][prettier] tools to format the source code. There are no custom rules; we just use the default formatting. You can format the code by running the `./format.ps1` command.

### Scripts

The scripts in this repository are written in PowerShell. As PowerShell is cross-platform nowadays, you can also install it on [Linux](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-6) and [macOS](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-macos?view=powershell-6).

[representer-introduction]: https://github.com/exercism/automated-analysis/blob/master/docs/representers/introduction.md
[representer-interface]: https://github.com/exercism/automated-analysis/blob/master/docs/representers/interface.md
[fsharp-compiler-services]: https://fsharp.github.io/FSharp.Compiler.Service/
[fantomas]: https://github.com/fsprojects/fantomas
[prettier]: https://prettier.io/
45 changes: 45 additions & 0 deletions bin/run-in-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env sh
set -e

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

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

# Output:
# Writes the representation to a representation.txt and representation.json file
# in the passed-in output directory.
# The output files 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 path/to/solution/folder/ path/to/output/directory/

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

slug="$1"
solution_dir=$(realpath "${2%/}")
output_dir=$(realpath "${3%/}")

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

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

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

# 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 files against the actual
# representation files generated by the test runner Docker image.

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

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

# Run the Docker image using the settings mimicking the production environment
docker run \
--rm \
--network none \
--read-only \
--mount type=bind,src="${PWD}/tests",dst=/opt/representer/tests \
--mount type=tmpfs,dst=/tmp \
--volume "${PWD}/bin/run-tests.sh:/opt/representer/bin/run-tests.sh" \
--workdir /opt/representer \
--entrypoint /opt/representer/bin/run-tests.sh \
exercism/fsharp-representer
37 changes: 37 additions & 0 deletions bin/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/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 files against the
# actual representation files generated by the test runner.

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

exit_code=0
filenames="representation.txt representation.json mapping.json"

# Iterate over all test directories
for test_dir in tests/*/*; do
test_dir_name=$(basename "${test_dir}")
test_dir_path=$(realpath "${test_dir}")

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

for filename in $filenames; do
actual_filepath="${test_dir_path}/${filename}"
expected_filepath="${test_dir_path}/expected_${filename}"

echo "${test_dir_name}: comparing ${filename} to expected_${filename}"
diff "${actual_filepath}" "${expected_filepath}"

if [ $? -ne 0 ]; then
exit_code=1
fi
done
done

exit ${exit_code}
26 changes: 26 additions & 0 deletions bin/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env sh

# Synopsis:
# Run the representer on a solution.

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

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

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

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

export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true
/opt/representer/Exercism.Representers.FSharp $1 $2 $3
3 changes: 0 additions & 3 deletions config.json

This file was deleted.

18 changes: 0 additions & 18 deletions format.ps1

This file was deleted.

27 changes: 0 additions & 27 deletions generate-in-bulk.ps1

This file was deleted.

33 changes: 0 additions & 33 deletions generate-in-docker.ps1

This file was deleted.

29 changes: 0 additions & 29 deletions generate.ps1

This file was deleted.

3 changes: 0 additions & 3 deletions run.sh

This file was deleted.

Loading

0 comments on commit 247f29b

Please sign in to comment.