Skip to content

Commit

Permalink
Add smoke tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Oct 26, 2023
1 parent 71a51cf commit b7df800
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 71 deletions.
37 changes: 16 additions & 21 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Run tests
on:
pull_request:
push:
branches: [ master, main ]
branches: [master, main]

jobs:
test:
Expand Down Expand Up @@ -36,32 +36,27 @@ jobs:
- name: Check formatting
run: elm-format src --validate

check-docker:
name: Check the docker container is built successfully
runs-on: ubuntu-latest
smoke_tests:
name: Run the Smoke Tests
runs-on: ubuntu-22.04
steps:
- name: Checkout code
uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f # 2.3.4
uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@154c24e1f33dbb5865a021c99f1318cfebf27b32 # 1.1.1

- name: Cache Docker layers
uses: actions/cache@0781355a23dac32fd3bac414512f4b903437991a # 2.1.3
uses: docker/setup-buildx-action@4c0219f9ac95b02789c1075625400b2acbff50b1
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
install: true

- name: Build Docker image
uses: docker/build-push-action@0db984c1826869dcd0740ff26ff75ff543238fd9 # 2.2.2
- name: Build Docker image and store in cache
uses: docker/build-push-action@2eb1c1961a95fc15694676618e422e8ba1d63825
with:
context: .
file: ./Dockerfile
push: false
tags: |
${{ github.event.repository.full_name }}:latest
${{ github.event.repository.full_name }}:${{ github.sha }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
load: true
tags: exercism/elm-representer
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Run Tests in Docker
run: bin/run-tests-in-docker.sh
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ src/main.js
.DS_Store
.coverage
/example-output/
/smoke-tests/*/mapping.json
/smoke-tests/*/representation.txt
/smoke-tests/**/*/mapping.json
/smoke-tests/**/*/representation.txt
/smoke-tests/**/*/Representation.elm
4 changes: 2 additions & 2 deletions bin/run-in-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ SLUG="$1"
INPUT_DIR="$2"
OUTPUT_DIR="$3"

docker build -t elm-representer .
docker build -t exercism/elm-representer .

mkdir -p "$OUTPUT_DIR"
docker run --network none \
--mount type=bind,src=$INPUT_DIR,dst=/solution \
--mount type=bind,src=$OUTPUT_DIR,dst=/output \
elm-representer $SLUG /solution/ /output/
exercism/elm-representer $SLUG /solution/ /output/
18 changes: 10 additions & 8 deletions bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,29 @@ INPUT_DIR="$2"
OUTPUT_DIR="$3"

# Normalize identifiers and remove comments
ELM_FILES=$(ls "$INPUT_DIR"/src/*.elm)
ELM_FILE_COUNT=$(ls "$INPUT_DIR"/src/*.elm | wc -l)
ELM_FILES=$(ls "${INPUT_DIR}"/src/*.elm)
ELM_FILE_COUNT=$(ls "${INPUT_DIR}"/src/*.elm | wc -l)
if [ $ELM_FILE_COUNT == 0 ]; then
echo "No .elm file found"
exit -1
elif [ $ELM_FILE_COUNT -gt 1 ]; then
echo "Multiple .elm files found: $ELM_FILES"
echo "Multiple .elm files found: ${ELM_FILES}"
exit -2
fi

ELM_FILEPATH=$ELM_FILES
ELM_FILENAME="$(basename $ELM_FILEPATH)"
ELM_FILEPATH="${ELM_FILES}"
ELM_REPRESENTATION_FILEPATH="${INPUT_DIR}/src/Representation.elm"
REPRESENTATION_FILEPATH="${OUTPUT_DIR}/representation.txt"
MAPPING_FILEPATH="${OUTPUT_DIR}/mapping.json"

echo "Normalizing identifiers in ${ELM_FILES}"
# This also creates the mapping.json file
cat "${ELM_FILES}" | node ./bin/cli.js ${OUTPUT_DIR}/mapping.json > "${ELM_FILES}"
cat "${ELM_FILES}" | node ./bin/cli.js "${MAPPING_FILEPATH}" > "${ELM_REPRESENTATION_FILEPATH}"

echo "Running elm-format"
./bin/elm-format $OUTPUT_DIR --yes
./bin/elm-format "${ELM_REPRESENTATION_FILEPATH}" --yes

echo "Creating representation.txt"
cat "${ELM_FILES}" > $OUTPUT_DIR/representation.txt
mv "${ELM_REPRESENTATION_FILEPATH}" "${REPRESENTATION_FILEPATH}"

echo Finished
25 changes: 0 additions & 25 deletions smoke-tests/anagram/.meta/config.json

This file was deleted.

9 changes: 7 additions & 2 deletions smoke-tests/anagram/expected_representation.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
module Anagram exposing (..)

import List as List exposing (filter, sort)
import String exposing (toList, toLower)
import String exposing (toList, toLower)


identifier_1 : String -> (List String -> List String)
identifier_1 identifier_2 =
List.filter (identifier_3 identifier_2) >> List.filter (identifier_4 identifier_2)


identifier_3 : String -> (String -> Bool)
identifier_3 identifier_2 identifier_5 =
identifier_6 identifier_2 == identifier_6 identifier_5


identifier_4 : String -> (String -> Bool)
identifier_4 identifier_7 identifier_8 =
String.toLower identifier_7 /= String.toLower identifier_8


identifier_6 : String -> List Char
identifier_6 =
identifier_6 =
String.toLower >> String.toList >> List.sort
23 changes: 12 additions & 11 deletions smoke-tests/anagram/src/Anagram.elm
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
-- This is a demonstration solution that I use when mentoring, any
-- This is a demonstration solution that I use when mentoring, any
-- improvements / comments / suggestions welcome.

-- I have changed the name of the exported function from `detect` to `filterAnagramsOf`,
-- as I think it describes the intent better. Most times when you are using this module,
-- as I think it describes the intent better. Most times when you are using this module,
-- this name / function definition will tell you all you need to know, and you won't need
-- to read anything else.

-- If you want to know how the algorithm works (the stipulation that idenitcal words be
-- removed is a debatable point for example) then you only have to read the body of
-- removed is a debatable point for example) then you only have to read the body of
-- `filterAnaagramsOf`.

-- So to understand what this does and how it does it there are only 4 lines to read,
-- So to understand what this does and how it does it there are only 4 lines to read,
-- which I think is powerful.

-- There is a solution available that uses more point free style


module Anagram exposing (..)

import List as List exposing (filter, sort)
import String exposing (toList, toLower)


filterAnagramsOf : String -> List String -> List String
filterAnagramsOf word =
List.filter (sortedLowercaseCharactersMatch word)
>> List.filter (notExactlyTheSameWord word)
>> List.filter (notExactlyTheSameWord word)


sortedLowercaseCharactersMatch : String -> String -> Bool
sortedLowercaseCharactersMatch word candidate =
sortedLowercaseCharacters word == sortedLowercaseCharacters candidate

notExactlyTheSameWord : String -> String -> Bool

notExactlyTheSameWord : String -> String -> Bool
notExactlyTheSameWord word1 word2 =
String.toLower word1 /= String.toLower word2


sortedLowercaseCharacters : String -> List Char
sortedLowercaseCharacters =
String.toLower
>> String.toList
>> List.sort
>> List.sort

0 comments on commit b7df800

Please sign in to comment.