From ba95985172674a48fb47b4b8a1f1158d7325cedb Mon Sep 17 00:00:00 2001 From: Jeremy Walker Date: Thu, 26 Oct 2023 12:00:16 +0900 Subject: [PATCH] Don't rely on filename for config reads (#47) * Don't rely on filename for config reads * Fix rubocop * Fix smoke tests --- lib/represent_solution.rb | 14 +++++++++++++- test/represent_solution_test.rb | 7 ++++++- test/representer_test.rb | 3 +++ tests/examples/basic/.meta/config.json | 1 + tests/examples/lasagna/.meta/config.json | 2 ++ 5 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/examples/basic/.meta/config.json create mode 100644 tests/examples/lasagna/.meta/config.json diff --git a/lib/represent_solution.rb b/lib/represent_solution.rb index e31bdc1..b5d99b2 100644 --- a/lib/represent_solution.rb +++ b/lib/represent_solution.rb @@ -1,10 +1,11 @@ +require 'json' + class RepresentSolution include Mandate initialize_with :exercise_slug, :solution_path, :output_path def call - code = File.read(solution_path / "#{exercise_slug.tr('-', '_')}.rb") representation = Representation.new(code) representation.normalize! @@ -16,4 +17,15 @@ def call f.write(representation.mapping.to_json) end end + + memoize + def code + filenames.map { |filename| File.read(solution_path / filename) }.join(" ") + end + + memoize + def filenames + config = JSON.parse(File.read(solution_path / '.meta' / 'config.json')) + config['files']['solution'] + end end diff --git a/test/represent_solution_test.rb b/test/represent_solution_test.rb index a9bdf04..efa9bfc 100644 --- a/test/represent_solution_test.rb +++ b/test/represent_solution_test.rb @@ -4,14 +4,19 @@ class RepresentateSolutionTest < Minitest::Test def test_extracts_and_writes_code_correctly code = "some code" ast = "some representation" + code_filepath = "foooooo.rb" mapping = { foo: 'bar' } representation = mock representation.stubs(ast:) representation.stubs(mapping:) representation.expects(:normalize!) + config = { files: { solution: [code_filepath] } }.to_json + Representation.expects(:new).with(code).returns(representation) - File.expects(:read).with(SOLUTION_PATH / "two_fer.rb").returns(code) + File.expects(:read).with(SOLUTION_PATH / ".meta/config.json").returns(config) + + File.expects(:read).with(SOLUTION_PATH / code_filepath).returns(code) writer = mock writer.expects(:write).with(ast) File.expects(:open). diff --git a/test/representer_test.rb b/test/representer_test.rb index f178c71..7bc150b 100644 --- a/test/representer_test.rb +++ b/test/representer_test.rb @@ -16,6 +16,9 @@ def test_that_it_converts_to_path_and_runs def test_e2e Dir.mktmpdir("code") do |dir| + Dir.mkdir("#{dir}/.meta") + File.write("#{dir}/.meta/config.json", { files: { solution: ['lasagna.rb'] } }.to_json) + File.write("#{dir}/lasagna.rb", ' class Lasagna EXPECTED_MINUTES_IN_OVEN = 40 diff --git a/tests/examples/basic/.meta/config.json b/tests/examples/basic/.meta/config.json new file mode 100644 index 0000000..7e8e7b0 --- /dev/null +++ b/tests/examples/basic/.meta/config.json @@ -0,0 +1 @@ +{ "files": { "solution": ["basic.rb"] } } diff --git a/tests/examples/lasagna/.meta/config.json b/tests/examples/lasagna/.meta/config.json new file mode 100644 index 0000000..3da9820 --- /dev/null +++ b/tests/examples/lasagna/.meta/config.json @@ -0,0 +1,2 @@ +{ "files": { "solution": ["lasagna.rb"] } } +