diff --git a/lib/utils/files.rb b/lib/utils/files.rb index 2e60505..5788f9a 100644 --- a/lib/utils/files.rb +++ b/lib/utils/files.rb @@ -12,21 +12,33 @@ def get_tree(sha) tree.tree end + def file_sha(path, branch) + files = list_files(branch) + files.select { |obj| obj['path'] == path }.first[:sha] + end + def list_files(branch) - branch_sha = @c.ref(@repo, "heads/#{branch}")[:object][:sha] - get_tree(branch_sha) + get_tree(branch_sha(branch)) + end + + def branch_sha(branch) + @c.ref(@repo, "heads/#{branch}")[:object][:sha] end def create_file(path, commit_msg, content, branch) @c.create_contents(@repo, path, commit_msg, content, branch: branch) end - # Get the contents of a file, note that ref can be a commit, branch or tag - def get_contents(path, ref) - @c.contents(@repo, path: path, ref: ref) + def delete_file(path, msg, sha, branch) + @c.delete_contents(@repo, path, msg, sha, branch: branch) end def update_contents(path, commit_msg, sha, content, branch) @c.update_contents(@repo, path, commit_msg, sha, content, branch: branch) end + + # Get the contents of a file, note that ref can be a commit, branch or tag + def get_contents(path, ref) + @c.contents(@repo, path: path, ref: ref) + end end diff --git a/test/suite.rb b/test/suite.rb index 524d21e..bd10335 100644 --- a/test/suite.rb +++ b/test/suite.rb @@ -3,6 +3,7 @@ require_relative 'test_committer' require_relative 'test_config' require_relative 'test_dotenv' +require_relative 'test_files' require_relative 'test_issues' require_relative 'test_pull_requests' require_relative 'test_queries' diff --git a/test/test_committer.rb b/test/test_committer.rb index 6634f44..56e6b77 100644 --- a/test/test_committer.rb +++ b/test/test_committer.rb @@ -1,8 +1,6 @@ require 'test/unit' require_relative '../lib/config' -Dotenv.load('orgbot.env') - class TestCommit < Test::Unit::TestCase def test_repo bot = CommitBot.new(ENV['REPO'], 'master', ENV['TOKEN']) diff --git a/test/test_files.rb b/test/test_files.rb new file mode 100644 index 0000000..32f3e98 --- /dev/null +++ b/test/test_files.rb @@ -0,0 +1,55 @@ +require 'test/unit' +require_relative '../lib/config' +require_relative '../lib/utils/files' + +class TestFiles < Test::Unit::TestCase + self.test_order = :defined + + def test_get_tree + files = FileBot.new(ENV['REPO'], ENV['TOKEN']) + assert_nothing_raised do + files.get_tree('master') + end + end + + def test_file_sha + files = FileBot.new(ENV['REPO'], ENV['TOKEN']) + assert_nothing_raised do + files.file_sha('README.md', 'master') + end + end + + def test_list_files + files = FileBot.new(ENV['REPO'], ENV['TOKEN']) + assert_nothing_raised do + files.list_files('master').inspect + end + end + + def test_branch_sha + files = FileBot.new(ENV['REPO'], ENV['TOKEN']) + assert_nothing_raised do + files.branch_sha('master') + end + end + + def test_create_update_delete_file + c = FileBot.new(ENV['REPO'], ENV['TOKEN']) + assert_nothing_raised do + path = 'testsuitefile.md' + msg = 'from test suite' + content = '2swag4u' + c.create_file(path, msg, content, 'master') + sha = c.file_sha(path, 'master') + c.update_contents(path, msg, sha, 'updated content', 'master') + c.delete_file(path, msg, c.file_sha(path, 'master'), 'master') + end + end + + def test_get_contents + files = FileBot.new(ENV['REPO'], ENV['TOKEN']) + assert_nothing_raised do + files.get_contents('README.md', 'master') + end + end +end