Skip to content

Commit

Permalink
Merge pull request #580 from Insti/Query_canonical-data_version
Browse files Browse the repository at this point in the history
Add query for version of canonical data
  • Loading branch information
hilary authored Apr 29, 2017
2 parents 9a73ca5 + 297b384 commit e15d63a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
6 changes: 5 additions & 1 deletion lib/generator/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/generator/files/metadata_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 22 additions & 9 deletions test/generator/files/metadata_files_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions test/generator/files_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down

0 comments on commit e15d63a

Please sign in to comment.