Skip to content

Commit

Permalink
Don't rely on filename for config reads (#47)
Browse files Browse the repository at this point in the history
* Don't rely on filename for config reads

* Fix rubocop

* Fix smoke tests
  • Loading branch information
iHiD authored Oct 26, 2023
1 parent 96684c7 commit ba95985
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/represent_solution.rb
Original file line number Diff line number Diff line change
@@ -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!

Expand All @@ -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
7 changes: 6 additions & 1 deletion test/represent_solution_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
3 changes: 3 additions & 0 deletions test/representer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/examples/basic/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "files": { "solution": ["basic.rb"] } }
2 changes: 2 additions & 0 deletions tests/examples/lasagna/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{ "files": { "solution": ["lasagna.rb"] } }

0 comments on commit ba95985

Please sign in to comment.