|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | + # Runs checkout command to checkout or create branch |
| 4 | + # |
| 5 | + # accepts options: |
| 6 | + # :new_branch |
| 7 | + # :force |
| 8 | + # :start_point |
| 9 | + # |
| 10 | + # @param [String] branch |
| 11 | + # @param [Hash] opts |
| 12 | + # def checkout(branch, opts = {}) |
| 13 | + |
| 14 | +class TestCheckout < Test::Unit::TestCase |
| 15 | + test 'checkout with no args' do |
| 16 | + expected_command_line = ['checkout'] |
| 17 | + git_cmd = :checkout |
| 18 | + git_cmd_args = [] |
| 19 | + assert_command_line(expected_command_line, git_cmd, git_cmd_args) |
| 20 | + end |
| 21 | + |
| 22 | + test 'checkout with no args and options' do |
| 23 | + expected_command_line = ['checkout', '--force'] |
| 24 | + git_cmd = :checkout |
| 25 | + git_cmd_args = [force: true] |
| 26 | + assert_command_line(expected_command_line, git_cmd, git_cmd_args) |
| 27 | + end |
| 28 | + |
| 29 | + test 'checkout with branch' do |
| 30 | + expected_command_line = ['checkout', 'feature1'] |
| 31 | + git_cmd = :checkout |
| 32 | + git_cmd_args = ['feature1'] |
| 33 | + assert_command_line(expected_command_line, git_cmd, git_cmd_args) |
| 34 | + end |
| 35 | + |
| 36 | + test 'checkout with branch and options' do |
| 37 | + expected_command_line = ['checkout', '--force', 'feature1'] |
| 38 | + git_cmd = :checkout |
| 39 | + git_cmd_args = ['feature1', force: true] |
| 40 | + assert_command_line(expected_command_line, git_cmd, git_cmd_args) |
| 41 | + end |
| 42 | + |
| 43 | + test 'checkout with branch name and new_branch: true' do |
| 44 | + expected_command_line = ['checkout', '-b', 'feature1'] |
| 45 | + git_cmd = :checkout |
| 46 | + git_cmd_args = ['feature1', new_branch: true] |
| 47 | + assert_command_line(expected_command_line, git_cmd, git_cmd_args) |
| 48 | + end |
| 49 | + |
| 50 | + test 'checkout with force: true' do |
| 51 | + expected_command_line = ['checkout', '--force', 'feature1'] |
| 52 | + git_cmd = :checkout |
| 53 | + git_cmd_args = ['feature1', force: true] |
| 54 | + assert_command_line(expected_command_line, git_cmd, git_cmd_args) |
| 55 | + end |
| 56 | + |
| 57 | + test 'checkout with branch name and new_branch: true and start_point: "sha"' do |
| 58 | + expected_command_line = ['checkout', '-b', 'feature1', 'sha'] |
| 59 | + git_cmd = :checkout |
| 60 | + git_cmd_args = ['feature1', new_branch: true, start_point: 'sha'] |
| 61 | + assert_command_line(expected_command_line, git_cmd, git_cmd_args) |
| 62 | + end |
| 63 | + |
| 64 | + |
| 65 | + test 'when checkout succeeds an error should not be raised' do |
| 66 | + in_temp_dir do |
| 67 | + git = Git.init('.', initial_branch: 'master') |
| 68 | + File.write('file1.txt', 'file1') |
| 69 | + git.add('file1.txt') |
| 70 | + git.commit('commit1') |
| 71 | + assert_nothing_raised { git.checkout('master') } |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + test 'when checkout fails a Git::FailedError should be raised' do |
| 76 | + in_temp_dir do |
| 77 | + git = Git.init('.', initial_branch: 'master') |
| 78 | + # fails because there are no commits |
| 79 | + assert_raises(Git::FailedError) { git.checkout('master') } |
| 80 | + end |
| 81 | + end |
| 82 | +end |
0 commit comments