Skip to content

Commit ba95985

Browse files
authored
Don't rely on filename for config reads (#47)
* Don't rely on filename for config reads * Fix rubocop * Fix smoke tests
1 parent 96684c7 commit ba95985

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

lib/represent_solution.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
require 'json'
2+
13
class RepresentSolution
24
include Mandate
35

46
initialize_with :exercise_slug, :solution_path, :output_path
57

68
def call
7-
code = File.read(solution_path / "#{exercise_slug.tr('-', '_')}.rb")
89
representation = Representation.new(code)
910
representation.normalize!
1011

@@ -16,4 +17,15 @@ def call
1617
f.write(representation.mapping.to_json)
1718
end
1819
end
20+
21+
memoize
22+
def code
23+
filenames.map { |filename| File.read(solution_path / filename) }.join(" ")
24+
end
25+
26+
memoize
27+
def filenames
28+
config = JSON.parse(File.read(solution_path / '.meta' / 'config.json'))
29+
config['files']['solution']
30+
end
1931
end

test/represent_solution_test.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ class RepresentateSolutionTest < Minitest::Test
44
def test_extracts_and_writes_code_correctly
55
code = "some code"
66
ast = "some representation"
7+
code_filepath = "foooooo.rb"
78
mapping = { foo: 'bar' }
89
representation = mock
910
representation.stubs(ast:)
1011
representation.stubs(mapping:)
1112
representation.expects(:normalize!)
1213

14+
config = { files: { solution: [code_filepath] } }.to_json
15+
1316
Representation.expects(:new).with(code).returns(representation)
14-
File.expects(:read).with(SOLUTION_PATH / "two_fer.rb").returns(code)
17+
File.expects(:read).with(SOLUTION_PATH / ".meta/config.json").returns(config)
18+
19+
File.expects(:read).with(SOLUTION_PATH / code_filepath).returns(code)
1520
writer = mock
1621
writer.expects(:write).with(ast)
1722
File.expects(:open).

test/representer_test.rb

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def test_that_it_converts_to_path_and_runs
1616

1717
def test_e2e
1818
Dir.mktmpdir("code") do |dir|
19+
Dir.mkdir("#{dir}/.meta")
20+
File.write("#{dir}/.meta/config.json", { files: { solution: ['lasagna.rb'] } }.to_json)
21+
1922
File.write("#{dir}/lasagna.rb", '
2023
class Lasagna
2124
EXPECTED_MINUTES_IN_OVEN = 40
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "files": { "solution": ["basic.rb"] } }
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{ "files": { "solution": ["lasagna.rb"] } }
2+

0 commit comments

Comments
 (0)