From ec66db7cb6bff678bcfe29b0cf573ce6083817d3 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sat, 29 Apr 2017 15:42:35 +0100 Subject: [PATCH 1/7] Add version method to CanonicalDataFile --- lib/generator/files/metadata_files.rb | 2 ++ test/generator/files/metadata_files_test.rb | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/generator/files/metadata_files.rb b/lib/generator/files/metadata_files.rb index b353d0e2c2..1dfca5981f 100644 --- a/lib/generator/files/metadata_files.rb +++ b/lib/generator/files/metadata_files.rb @@ -17,6 +17,8 @@ def exercise_metadata_path end class CanonicalDataFile < Readable + def version + end end end end diff --git a/test/generator/files/metadata_files_test.rb b/test/generator/files/metadata_files_test.rb index 5a18369cc2..6bca07a1e1 100644 --- a/test/generator/files/metadata_files_test.rb +++ b/test/generator/files/metadata_files_test.rb @@ -21,5 +21,12 @@ def test_canonical_data 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 + end end end From f65859977d7317eebe0dfc89018e75df22d73651 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sat, 29 Apr 2017 15:46:42 +0100 Subject: [PATCH 2/7] Get the version from a file that has it. The version string comes from the 'version' key of the json file. --- lib/generator/files/metadata_files.rb | 1 + test/generator/files/metadata_files_test.rb | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/lib/generator/files/metadata_files.rb b/lib/generator/files/metadata_files.rb index 1dfca5981f..b6b5b173a7 100644 --- a/lib/generator/files/metadata_files.rb +++ b/lib/generator/files/metadata_files.rb @@ -18,6 +18,7 @@ def exercise_metadata_path class CanonicalDataFile < Readable def version + JSON.parse(to_s)['version'] if to_s end end end diff --git a/test/generator/files/metadata_files_test.rb b/test/generator/files/metadata_files_test.rb index 6bca07a1e1..0400da676c 100644 --- a/test/generator/files/metadata_files_test.rb +++ b/test/generator/files/metadata_files_test.rb @@ -27,6 +27,11 @@ 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: 'test/fixtures/metadata/exercises/complex/canonical-data.json') + assert_equal "1.0.0", subject.version + end end end end From 8638288c62755f9841ee77785d7f1c898c4f63df Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sat, 29 Apr 2017 15:49:36 +0100 Subject: [PATCH 3/7] Test for canonical-data file without a version. --- test/generator/files/metadata_files_test.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/generator/files/metadata_files_test.rb b/test/generator/files/metadata_files_test.rb index 0400da676c..79d16f559d 100644 --- a/test/generator/files/metadata_files_test.rb +++ b/test/generator/files/metadata_files_test.rb @@ -32,6 +32,11 @@ def test_version subject = CanonicalDataFile.new(filename: 'test/fixtures/metadata/exercises/complex/canonical-data.json') assert_equal "1.0.0", subject.version end + + def test_version_not_present + subject = CanonicalDataFile.new(filename: 'test/fixtures/metadata/exercises/alpha/canonical-data.json') + assert_nil subject.version + end end end end From 9dd39cec9831ed2ea40d9a6e7074d5c47209edf9 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sat, 29 Apr 2017 18:14:20 +0100 Subject: [PATCH 4/7] Extract Generator::Files#read method. Create a new method on `Files` to return the contents of a file or nil if it doesn't exist. This is useful so we can stub it out in the tests when we need to pretend to read a file. Another option would be to refine `File` but I think having it on Generator::Files makes it a little more clear where the method is coming from. --- lib/generator/files.rb | 6 +++++- test/generator/files_test.rb | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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/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'] From 17c9f590d40e22a50928c0f451000ce612533fc9 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sat, 29 Apr 2017 18:16:37 +0100 Subject: [PATCH 5/7] Stub file read for no version key --- test/generator/files/metadata_files_test.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/generator/files/metadata_files_test.rb b/test/generator/files/metadata_files_test.rb index 79d16f559d..fb5da4089b 100644 --- a/test/generator/files/metadata_files_test.rb +++ b/test/generator/files/metadata_files_test.rb @@ -34,8 +34,10 @@ def test_version end def test_version_not_present - subject = CanonicalDataFile.new(filename: 'test/fixtures/metadata/exercises/alpha/canonical-data.json') - assert_nil subject.version + subject = CanonicalDataFile.new(filename: 'no version key') + Files.stub(:read, '{ "json": true }' ) do + assert_nil subject.version + end end end end From 66aa75fdd31fe2b595e8297561d374a682054289 Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sat, 29 Apr 2017 16:02:53 +0100 Subject: [PATCH 6/7] Stub Files#read for the test of a file with a version. --- test/generator/files/metadata_files_test.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/generator/files/metadata_files_test.rb b/test/generator/files/metadata_files_test.rb index fb5da4089b..03bf788d3e 100644 --- a/test/generator/files/metadata_files_test.rb +++ b/test/generator/files/metadata_files_test.rb @@ -29,8 +29,10 @@ def test_version_for_file_that_does_not_exist end def test_version - subject = CanonicalDataFile.new(filename: 'test/fixtures/metadata/exercises/complex/canonical-data.json') - assert_equal "1.0.0", subject.version + subject = CanonicalDataFile.new(filename: 'has version key') + Files.stub(:read, '{"version": "1.2.3"}' ) do + assert_equal "1.2.3", subject.version + end end def test_version_not_present From 297b3840c12ebcf7585f427fdec7ab5ec0102a1b Mon Sep 17 00:00:00 2001 From: Geoff Hubbard Date: Sat, 29 Apr 2017 18:25:38 +0100 Subject: [PATCH 7/7] Simplify test for MetadataFiles#canonical_data --- test/generator/files/metadata_files_test.rb | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/test/generator/files/metadata_files_test.rb b/test/generator/files/metadata_files_test.rb index 03bf788d3e..48c6089491 100644 --- a/test/generator/files/metadata_files_test.rb +++ b/test/generator/files/metadata_files_test.rb @@ -7,17 +7,9 @@ class MetadataFilesTest < Minitest::Test track: 'test/fixtures/xruby' ) - class TestMetadataFiles - def initialize - @paths = FixturePaths - @exercise_name = 'alpha' - end - attr_reader :paths, :exercise_name - include MetadataFiles - end - def test_canonical_data - subject = TestMetadataFiles.new + subject = OpenStruct.new(paths: FixturePaths, exercise_name: 'unimportant') + subject.extend(MetadataFiles) assert_instance_of CanonicalDataFile, subject.canonical_data end end