Skip to content

Commit

Permalink
increase test amazingness
Browse files Browse the repository at this point in the history
  • Loading branch information
tcob committed Apr 17, 2018
1 parent 67b00c1 commit ad50fda
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
22 changes: 17 additions & 5 deletions lib/utils/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions test/suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 0 additions & 2 deletions test/test_committer.rb
Original file line number Diff line number Diff line change
@@ -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'])
Expand Down
55 changes: 55 additions & 0 deletions test/test_files.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ad50fda

Please sign in to comment.