5
5
require "open3"
6
6
7
7
module CommandHelpers
8
- Command = Struct . new ( :exit_status , :output , :error )
8
+ class SystemCallFailure < StandardError ; end
9
+ Command = Struct . new ( :exit_status , :output , :error ) do
10
+ def failed?
11
+ exit_status != 0 && !error . to_s . empty?
12
+ end
13
+
14
+ def error_message_with_exit_status
15
+ "[exit status: #{ exit_status } ] #{ error } "
16
+ end
17
+ end
9
18
10
19
def fixtures_dir
11
20
File . expand_path "#{ Dir . pwd } /spec/fixtures"
@@ -31,6 +40,7 @@ def git_cmd_path
31
40
def setup_environment
32
41
setup_home_dir
33
42
setup_project_dir
43
+ initialize_git_user
34
44
initialize_git_repo
35
45
end
36
46
@@ -48,21 +58,26 @@ def setup_project_dir
48
58
FileUtils . mkdir_p project_dir
49
59
end
50
60
61
+ def initialize_git_user
62
+ run_system_call 'git config --global user.name "GitHub Actions Bot"' , fail_on_error : true
63
+ run_system_call 'git config --global user.email "<>"' , fail_on_error : true
64
+ end
65
+
51
66
def initialize_git_repo
52
- run_system_call "touch README.md"
53
- run_system_call "git init ."
54
- run_system_call "git add README.md"
55
- run_system_call 'git commit -am "Initial commit"'
67
+ run_system_call "touch README.md" , fail_on_error : true
68
+ run_system_call "git init -b main ." , fail_on_error : true
69
+ run_system_call "git add README.md" , fail_on_error : true
70
+ run_system_call 'git commit -am "Initial commit"' , fail_on_error : true
56
71
end
57
72
58
- def setup_working_branch ( branch = "master " )
59
- run_system_call "git checkout -b #{ branch } "
73
+ def setup_working_branch ( branch = "main " )
74
+ run_system_call "git checkout -b #{ branch } " , fail_on_error : true
60
75
end
61
76
62
77
def make_commit ( message = "Changes" )
63
- run_system_call 'echo "Changes" >> README.md'
64
- run_system_call "git add README.md"
65
- run_system_call "git commit -am \" #{ message } \" "
78
+ run_system_call 'echo "Changes" >> README.md' , fail_on_error : true
79
+ run_system_call "git add README.md" , fail_on_error : true
80
+ run_system_call "git commit -am \" #{ message } \" " , fail_on_error : true
66
81
end
67
82
68
83
def command_helpers_teardown
@@ -89,16 +104,20 @@ def capture_io
89
104
$stderr = orig_stderr
90
105
end
91
106
92
- def run_system_call ( command_string )
107
+ def run_system_call ( command_string , fail_on_error : false )
93
108
last_command . output , last_command . error , last_command . exit_status = run_in_test_context do
94
109
Open3 . capture3 ( command_string )
95
110
end
111
+
112
+ return unless fail_on_error && last_command . failed?
113
+
114
+ raise SystemCallFailure , <<~ERROR_MSG
115
+ System call '#{ command_string } ' failed to run: #{ last_command . error_message_with_exit_status }
116
+ ERROR_MSG
96
117
end
97
118
98
- def run_in_test_context ( &_block )
99
- Dir . chdir project_dir do
100
- yield
101
- end
119
+ def run_in_test_context ( &block )
120
+ Dir . chdir ( project_dir , &block )
102
121
end
103
122
104
123
private
0 commit comments