diff --git a/lib/generator/files.rb b/lib/generator/files.rb index 70822a9415..06e3785f68 100644 --- a/lib/generator/files.rb +++ b/lib/generator/files.rb @@ -2,6 +2,10 @@ module Generator module Files + def self.read(filename) + File.read(filename) if File.exist?(filename) + end + class Readable attr_reader :filename, :repository_root def initialize(filename:, repository_root: nil) @@ -10,7 +14,7 @@ def initialize(filename:, repository_root: nil) end def to_s - File.read(filename) if File.exist?(filename) + Files.read(filename) end def abbreviated_commit_hash diff --git a/lib/generator/files/metadata_files.rb b/lib/generator/files/metadata_files.rb index b353d0e2c2..b6b5b173a7 100644 --- a/lib/generator/files/metadata_files.rb +++ b/lib/generator/files/metadata_files.rb @@ -17,6 +17,9 @@ def exercise_metadata_path end class CanonicalDataFile < Readable + def version + JSON.parse(to_s)['version'] if to_s + end end end end diff --git a/test/generator/files/metadata_files_test.rb b/test/generator/files/metadata_files_test.rb index 5a18369cc2..48c6089491 100644 --- a/test/generator/files/metadata_files_test.rb +++ b/test/generator/files/metadata_files_test.rb @@ -7,18 +7,31 @@ class MetadataFilesTest < Minitest::Test track: 'test/fixtures/xruby' ) - class TestMetadataFiles - def initialize - @paths = FixturePaths - @exercise_name = 'alpha' + def test_canonical_data + subject = OpenStruct.new(paths: FixturePaths, exercise_name: 'unimportant') + subject.extend(MetadataFiles) + assert_instance_of CanonicalDataFile, subject.canonical_data + end + end + + class CanonicalDataFileTest < Minitest::Test + def test_version_for_file_that_does_not_exist + subject = CanonicalDataFile.new(filename: 'nonexistant') + assert_nil subject.version + end + + def test_version + subject = CanonicalDataFile.new(filename: 'has version key') + Files.stub(:read, '{"version": "1.2.3"}' ) do + assert_equal "1.2.3", subject.version end - attr_reader :paths, :exercise_name - include MetadataFiles end - def test_canonical_data - subject = TestMetadataFiles.new - assert_instance_of CanonicalDataFile, subject.canonical_data + def test_version_not_present + subject = CanonicalDataFile.new(filename: 'no version key') + Files.stub(:read, '{ "json": true }' ) do + assert_nil subject.version + end end end end diff --git a/test/generator/files_test.rb b/test/generator/files_test.rb index a4385fa968..9a11f97220 100644 --- a/test/generator/files_test.rb +++ b/test/generator/files_test.rb @@ -2,6 +2,24 @@ module Generator module Files + + class ReadTest < Minitest::Test + def test_read_existing_file + expected = 'content unimportant' + File.stub(:exist?, true) do + File.stub(:read, expected ) do + assert_equal expected, Files.read('pretend/this/exists') + end + end + end + + def test_read_non_existing_file + File.stub(:exist?, false) do + assert_nil Files.read('pretend/this/does/not/exist') + end + end + end + class ReadableTest < Minitest::Test def test_abbreviated_commit_hash mock_git_command = Minitest::Mock.new.expect :call, nil, ['path/.git', 'subdir/file']