Skip to content

Commit

Permalink
Store full response
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Oct 15, 2021
1 parent dba2d15 commit d726023
Show file tree
Hide file tree
Showing 57 changed files with 103 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules/
tests/**/counts.json
tests/**/response.json
tests/**/.tokeignore
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ To count the lines of code of an arbitrary solution, do the following:
1. Open a terminal in the project's root
2. Run `./bin/run.sh <track-slug> <exercise-slug> <solution-dir> <output-dir>`

Once the test runner has finished, its results will be written to `<output-dir>/counts.json`.
Once the test runner has finished, its results will be written to `<output-dir>/response.json`.

## Run the Lines of Code Counter on a solution using Docker

Expand All @@ -104,7 +104,7 @@ To count the lines of code of an arbitrary solution using the Docker image, do t
1. Open a terminal in the project's root
2. Run `./bin/run-in-docker.sh <track-slug> <exercise-slug> <solution-dir> <output-dir>`

Once the test runner has finished, its results will be written to `<output-dir>/counts.json`.
Once the test runner has finished, its results will be written to `<output-dir>/response.json`.

## Run the tests

Expand All @@ -113,9 +113,9 @@ To run the tests to verify the behavior of the Lines of Code Counter, do the fol
1. Open a terminal in the project's root
2. Run `./bin/run-tests.sh`

These are [golden tests][golden] that compare the `counts.json` generated by running the current state of the code against the "known good" `tests/<test-name>/counts.json`. All files created during the test run itself are discarded.
These are [golden tests][golden] that compare the `response.json` generated by running the current state of the code against the "known good" `tests/<test-name>/response.json`. All files created during the test run itself are discarded.

When you've made modifications to the code that will result in a new "golden" state, you'll need to generate and commit a new `tests/<test-name>/counts.json` file.
When you've made modifications to the code that will result in a new "golden" state, you'll need to generate and commit a new `tests/<test-name>/response.json` file.

## Run the tests using Docker

Expand All @@ -126,9 +126,9 @@ To run the tests to verify the behavior of the test runner using the Docker imag
1. Open a terminal in the project's root
2. Run `./bin/run-tests-in-docker.sh`

These are [golden tests][golden] that compare the `counts.json` generated by running the current state of the code against the "known good" `tests/<test-name>/counts.json`. All files created during the test run itself are discarded.
These are [golden tests][golden] that compare the `response.json` generated by running the current state of the code against the "known good" `tests/<test-name>/response.json`. All files created during the test run itself are discarded.

When you've made modifications to the code that will result in a new "golden" state, you'll need to generate and commit a new `tests/<test-name>/counts.json` file.
When you've made modifications to the code that will result in a new "golden" state, you'll need to generate and commit a new `tests/<test-name>/response.json` file.

## Credit

Expand Down
22 changes: 8 additions & 14 deletions bin/run-in-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,40 @@
# $1: track slug
# $2: exercise slug
# $3: path to solution folder
# $4: path to output directory

# Output:
# Count the lines of code of the solution in the passed-in output directory.
# Count the lines of code of the solution in the passed-in solution directory.

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

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

track_slug="${1}"
exercise_slug="${2}"
solution_dir=$(realpath "${3%/}")
output_dir=$(realpath "${4%/}")
results_file="${output_dir}/counts.json"
response_file="${solution_dir}/response.json"
container_port=9876

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

# Build the Docker image
docker build --progress=plain --rm -t exercism/lines-of-code-counter .
docker build --rm -t exercism/lines-of-code-counter .

# Run the Docker image using the settings mimicking the production environment
container_id=$(docker run \
--detach \
--publish ${container_port}:8080 \
--mount type=bind,src="${solution_dir}",dst=/solution \
--mount type=bind,src="${output_dir}",dst=/output \
exercism/lines-of-code-counter)

echo "${track_slug}/${exercise_slug}: counting lines of code..."

# Call the function with the correct JSON event payload
event_json=$(jq -n --arg t "${track_slug}" --arg e "${exercise_slug}" '{track: $t, exercise: $e, solution: "/solution", output: "/output"}')
curl --silent --output /dev/null -XPOST http://localhost:${container_port}/2015-03-31/functions/function/invocations -d "${event_json}"
event_json=$(jq -n --arg t "${track_slug}" --arg e "${exercise_slug}" '{track: $t, exercise: $e, solution: "/solution"}')
curl --silent --output "${response_file}" -XPOST http://localhost:${container_port}/2015-03-31/functions/function/invocations -d "${event_json}"

echo "${track_slug}/${exercise_slug}: done"

Expand Down
16 changes: 10 additions & 6 deletions bin/run-tests-in-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ for test_dir in tests/${track_slug}/${exercise_slug}; do
track_name=$(basename $(realpath "${test_dir}/../"))
exercise_name=$(basename "${test_dir}")
test_dir_path=$(realpath "${test_dir}")
counts_file_path="${test_dir_path}/counts.json"
expected_counts_file_path="${test_dir_path}/expected_counts.json"
response_file_path="${test_dir_path}/response.json"
expected_response_file_path="${test_dir_path}/expected_response.json"

rm -rf "${counts_file_path}"
rm -rf "${response_file_path}"

bin/run-in-docker.sh "${track_name}" "${exercise_name}" "${test_dir_path}" "${test_dir_path}"
bin/run-in-docker.sh "${track_name}" "${exercise_name}" "${test_dir_path}"

echo "${track_name}/${exercise_name}: comparing counts.json to expected_counts.json"
diff "${counts_file_path}" "${expected_counts_file_path}"
# Ensure there is a trailing newline in both files
sed -i -e '$a\' "${response_file_path}"
sed -i -e '$a\' "${expected_response_file_path}"

echo "${track_name}/${exercise_name}: comparing response.json to expected_response.json"
diff "${response_file_path}" "${expected_response_file_path}"

if [ $? -ne 0 ]; then
exit_code=1
Expand Down
18 changes: 11 additions & 7 deletions bin/run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /bin/sh
#! /bin/bash

# Synopsis:
# Test the lines of code counter by running it against a predefined set of solutions
Expand Down Expand Up @@ -33,15 +33,19 @@ for test_dir in tests/${track_slug}/${exercise_slug}; do
track_name=$(basename $(realpath "${test_dir}/../"))
exercise_name=$(basename "${test_dir}")
test_dir_path=$(realpath "${test_dir}")
counts_file_path="${test_dir_path}/counts.json"
expected_counts_file_path="${test_dir_path}/expected_counts.json"
response_file_path="${test_dir_path}/response.json"
expected_response_file_path="${test_dir_path}/expected_response.json"

rm -rf "${counts_file_path}"
rm -rf "${response_file_path}"

bin/run.sh "${track_name}" "${exercise_name}" "${test_dir_path}" "${test_dir_path}"
bin/run.sh "${track_name}" "${exercise_name}" "${test_dir_path}"

echo "${track_name}/${exercise_name}: comparing counts.json to expected_counts.json"
diff "${counts_file_path}" "${expected_counts_file_path}"
# Ensure there is a trailing newline in both files
sed -i -e '$a\' "${response_file_path}"
sed -i -e '$a\' "${expected_response_file_path}"

echo "${track_name}/${exercise_name}: comparing response.json to expected_response.json"
diff "${response_file_path}" "${expected_response_file_path}"

if [ $? -ne 0 ]; then
exit_code=1
Expand Down
7 changes: 4 additions & 3 deletions bin/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
event = {
"track" => ARGV[0],
"exercise" => ARGV[1],
"solution" => ARGV[2],
"output" => ARGV[3]
"solution" => ARGV[2]
}
LinesOfCodeCounter.process(event: event, context: {})

response = LinesOfCodeCounter.process(event: event, context: {})
puts response.to_json
16 changes: 6 additions & 10 deletions bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,27 @@
# $1: track slug
# $2: exercise slug
# $3: path to solution folder
# $4: path to output directory

# Output:
# Count the lines of code of the solution in the passed-in output directory.
# Count the lines of code of the solution in the passed-in solution directory.

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

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

track_slug="${1}"
exercise_slug="${2}"
solution_dir=$(realpath "${3%/}")
output_dir=$(realpath "${4%/}")

# Create the output directory if it doesn't exist
mkdir -p "${output_dir}"
response_file="${solution_dir}/response.json"

echo "${track_slug}/${exercise_slug}: counting lines of code..."

# Call the function with the correct JSON event payload
ruby "./bin/run.rb" "${track_slug}" "${exercise_slug}" "${solution_dir}" "${output_dir}"
ruby "./bin/run.rb" "${track_slug}" "${exercise_slug}" "${solution_dir}" > "${response_file}"

echo "${track_slug}/${exercise_slug}: done"
8 changes: 8 additions & 0 deletions lib/lines_of_code_counter/count_lines_of_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ class CountLinesOfCode
initialize_with :exercise

def call
File.write(ignore_file.filepath, ignore_file.content)
{
code: code,
blanks: blanks,
comments: comments,
files: files
}
ensure
FileUtils.rm(ignore_file.filepath)
end

private
Expand Down Expand Up @@ -37,4 +40,9 @@ def files
sort.
to_a
end

memoize
def ignore_file
IgnoreFile.new(exercise)
end
end
5 changes: 3 additions & 2 deletions lib/lines_of_code_counter/exercise.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Exercise
include Mandate

attr_reader :dir
attr_reader :track, :dir

def initialize(dir)
def initialize(track, dir)
@track = track
@dir = dir
end

Expand Down
14 changes: 10 additions & 4 deletions lib/lines_of_code_counter/ignore_file.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class IgnoreFile
include Mandate

initialize_with :track, :exercise
initialize_with :exercise

def filepath
File.join(exercise.dir, ".tokeignore")
Expand All @@ -24,8 +24,7 @@ def track_specific_rules
*exercise.example_files,
*exercise.exemplar_files,
*exercise.editor_files,
"counts.json",
"expected_counts.json"
*test_file_rules
]
end

Expand All @@ -36,8 +35,15 @@ def default_rules
]
end

def test_file_rules
[
"response.json",
"expected_response.json"
]
end

private
def track_ignore_filepath
"tracks/#{track}.ignore"
"tracks/#{exercise.track}.ignore"
end
end
41 changes: 13 additions & 28 deletions lib/lines_of_code_counter/process_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,22 @@ class ProcessRequest

initialize_with :event, :content

def call
File.write(ignore_file.filepath, ignore_file.content)
def call
counts = CountLinesOfCode.(exercise)
File.write(counts_filepath, counts.to_json)
# FileUtils.rm(ignore_file.filepath)
counts_json = counts.to_json

{
statusCode: 200,
headers: {
'Content-Length': counts_json.bytesize,
'Content-Type': 'application/json; charset=utf-8'
},
isBase64Encoded: false,
body: counts_json
}
end

private
def track
event["track"]
end

def solution_dir
event["solution"]
end

def output_dir
event["output"]
end

def counts_filepath
File.join(output_dir, "counts.json")
end

memoize
def exercise
Exercise.new(solution_dir)
end

memoize
def ignore_file
IgnoreFile.new(track, exercise)
Exercise.new(event["track"], event["solution"])
end
end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":54,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":27,\"blanks\":3,\"comments\":0,\"files\":[\"Bob.cs\"]}"}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":85,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":45,\"blanks\":4,\"comments\":0,\"files\":[\"AnotherHelper.cs\",\"Bob.cs\",\"Helper.cs\"]}"}
1 change: 0 additions & 1 deletion tests/csharp/multiple-editor-files/expected_counts.json

This file was deleted.

1 change: 1 addition & 0 deletions tests/csharp/multiple-editor-files/expected_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":54,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":27,\"blanks\":3,\"comments\":0,\"files\":[\"Bob.cs\"]}"}
1 change: 0 additions & 1 deletion tests/csharp/multiple-solution-files/expected_counts.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":65,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":34,\"blanks\":3,\"comments\":0,\"files\":[\"Bob.cs\",\"Bobby.cs\"]}"}
1 change: 0 additions & 1 deletion tests/csharp/old-test-file-name/expected_counts.json

This file was deleted.

1 change: 1 addition & 0 deletions tests/csharp/old-test-file-name/expected_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":54,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":27,\"blanks\":3,\"comments\":0,\"files\":[\"Bob.cs\"]}"}
1 change: 0 additions & 1 deletion tests/csharp/single-additional-file/expected_counts.json

This file was deleted.

1 change: 1 addition & 0 deletions tests/csharp/single-additional-file/expected_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":66,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":42,\"blanks\":5,\"comments\":0,\"files\":[\"Bob.cs\",\"Helper.cs\"]}"}
1 change: 0 additions & 1 deletion tests/csharp/single-editor-file/expected_counts.json

This file was deleted.

1 change: 1 addition & 0 deletions tests/csharp/single-editor-file/expected_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":54,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":27,\"blanks\":3,\"comments\":0,\"files\":[\"Bob.cs\"]}"}
1 change: 0 additions & 1 deletion tests/csharp/single-solution-file/expected_counts.json

This file was deleted.

1 change: 1 addition & 0 deletions tests/csharp/single-solution-file/expected_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":54,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":27,\"blanks\":3,\"comments\":0,\"files\":[\"Bob.cs\"]}"}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":57,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":8,\"blanks\":4,\"comments\":0,\"files\":[\"Anagram.fs\"]}"}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":86,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":15,\"blanks\":8,\"comments\":0,\"files\":[\"Anagram.fs\",\"Custom.fs\",\"MoreCustom.fs\"]}"}
1 change: 0 additions & 1 deletion tests/fsharp/multiple-editor-files/expected_counts.json

This file was deleted.

1 change: 1 addition & 0 deletions tests/fsharp/multiple-editor-files/expected_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":57,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":8,\"blanks\":4,\"comments\":0,\"files\":[\"Anagram.fs\"]}"}
1 change: 0 additions & 1 deletion tests/fsharp/multiple-solution-files/expected_counts.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Content-Length":70,"Content-Type":"application/json; charset=utf-8"},"isBase64Encoded":false,"body":"{\"code\":13,\"blanks\":5,\"comments\":0,\"files\":[\"Anagram.fs\",\"Helper.fs\"]}"}
1 change: 0 additions & 1 deletion tests/fsharp/old-test-name/expected_counts.json

This file was deleted.

Loading

0 comments on commit d726023

Please sign in to comment.