Skip to content

Commit

Permalink
Merge pull request #31 from Genki-S/run_normal_tests_when_no_mapping_…
Browse files Browse the repository at this point in the history
…is_found

Run all tests when no mapping ('.ttnt' file) is found
  • Loading branch information
Genki-S committed Aug 3, 2015
2 parents aaf4879 + c7d316d commit 98a3600
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
7 changes: 6 additions & 1 deletion lib/ttnt/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ def filename

def read_storage_content
if @sha
@repo.lookup(@repo.lookup(@sha).tree['.ttnt'][:oid]).content
tree = @repo.lookup(@sha).tree
if tree['.ttnt']
@repo.lookup(@repo.lookup(@sha).tree['.ttnt'][:oid]).content
else
'' # Storage file is not committed for the commit of given sha
end
else
File.exist?(filename) ? File.read(filename) : ''
end
Expand Down
13 changes: 10 additions & 3 deletions lib/ttnt/test_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class TestSelector
# @param test_files [#include?] candidate test files
def initialize(repo, target_sha, test_files)
@repo = repo
@metadata = MetaData.new(repo, target_sha)
storage_src_sha = target_sha ? target_sha : @repo.head.target_id
@metadata = MetaData.new(repo, storage_src_sha)
@target_obj = @repo.lookup(target_sha) if target_sha

# Base should be the commit `ttnt:anchor` has run on.
Expand All @@ -30,7 +31,9 @@ def initialize(repo, target_sha, test_files)
#
# @return [Set] a set of tests that might be affected by changes in base_sha...target_sha
def select_tests!
# TODO: if test-to-code-mapping is not found (ttnt-anchor has not been run)
# select all tests if anchored commit does not exist
return Set.new(@test_files) unless @base_obj

@tests ||= Set.new
diff = @target_obj ? @base_obj.diff(@target_obj) : @base_obj.diff_workdir
diff.each_patch do |patch|
Expand Down Expand Up @@ -83,7 +86,11 @@ def select_tests_from_patch(patch)

# Find the commit `rake ttnt:test:anchor` has been run on.
def find_anchored_commit
@repo.lookup(@metadata['anchored_commit'])
if @metadata['anchored_commit']
@repo.lookup(@metadata['anchored_commit'])
else
nil
end
end

# Check if given file is a test file.
Expand Down
22 changes: 15 additions & 7 deletions test/helpers/git_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@ module TTNT
module GitHelper
module_function

def git_commit_am(message)
index = @repo.index
def git_commit(index, message)
options = {}

index.read_tree(@repo.head.target.tree) unless @repo.empty?
index.add_all

options[:tree] = index.write_tree(@repo)
options[:author] = { email: "foo@bar.com", name: 'Author', time: Time.now }
options[:committer] = options[:author]
options[:message] = message
options[:parents] = @repo.empty? ? [] : [@repo.head.target].compact
options[:update_ref] = 'HEAD'

Rugged::Commit.create(@repo, options)
index.write
end

def git_commit_am(message)
index = @repo.index
index.read_tree(@repo.head.target.tree) unless @repo.empty?
index.add_all
git_commit(index, message)
end

def git_rm_and_commit(file, message)
index = @repo.index
index.read_tree(@repo.head.target.tree) unless @repo.empty?
index.remove(file.gsub(/^#{@repo.workdir}\//, ''))
git_commit(index, message)
end

def git_checkout_b(branch)
@repo.create_branch(branch)
@repo.checkout(branch)
Expand Down
7 changes: 7 additions & 0 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def test_no_test_is_selected
assert_equal "", output[:stdout]
end

def test_all_tests_are_selected_without_mapping
git_rm_and_commit("#{@repo.workdir}/.ttnt", 'Remove .ttnt')
rake_ttnt_result = rake('ttnt:test:run')[:stdout].lines.last
rake_test_result = rake('test')[:stdout].lines.last
assert_equal rake_test_result, rake_ttnt_result
end

def test_fizz_test_is_selected
@repo.checkout('change_fizz')
output = rake('ttnt:test:run')
Expand Down
6 changes: 6 additions & 0 deletions test/storage_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def test_read_storage_from_history
'History storage should not contain data from current working directory.'
end

def test_read_absent_storage_from_history
git_rm_and_commit("#{@repo.workdir}/.ttnt", 'Remove .ttnt file')
storage = TTNT::Storage.new(@repo, @repo.head.target_id)
assert_equal Hash.new, storage.read(@section)
end

def test_write_storage
@storage.write!(@section, @data)
assert File.exist?(@storage_file), 'Storage file should be created.'
Expand Down
6 changes: 6 additions & 0 deletions test/test_selector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@ def test_selects_tests_with_changed_test_file
selector = TTNT::TestSelector.new(@repo, target_sha, @test_files)
assert_includes selector.select_tests!, 'buzz_test.rb'
end

def test_selects_all_tests_with_no_anchored_commit
git_rm_and_commit("#{@repo.workdir}/.ttnt", 'Remove .ttnt file')
selector = TTNT::TestSelector.new(@repo, @repo.head.target_id, @test_files)
assert_equal Set.new(['fizz_test.rb', 'buzz_test.rb']), selector.select_tests!
end
end
end

0 comments on commit 98a3600

Please sign in to comment.