@@ -97,4 +97,67 @@ def with_custom_env_variables(&block)
97
97
Git ::Lib ::ENV_VARIABLE_NAMES . each { |k | ENV [ k ] = saved_env [ k ] }
98
98
end
99
99
end
100
+
101
+ # Assert that the expected command line args are generated for a given Git::Lib method
102
+ #
103
+ # This assertion generates an empty git repository and then runs calls
104
+ # Git::Base method named by `git_cmd` passing that method `git_cmd_args`.
105
+ #
106
+ # Before calling `git_cmd`, this method stubs the `Git::Lib#command` method to
107
+ # capture the args sent to it by `git_cmd`. These args are captured into
108
+ # `actual_command_line`.
109
+ #
110
+ # assert_equal is called comparing the given `expected_command_line` to
111
+ # `actual_command_line`.
112
+ #
113
+ # @example Fetch with no args
114
+ # expected_command_line = ['fetch', '--', 'origin']
115
+ # git_cmd = :fetch
116
+ # git_cmd_args = []
117
+ # assert_command_line(expected_command_line, git_cmd, git_cmd_args)
118
+ #
119
+ # @example Fetch with some args
120
+ # expected_command_line = ['fetch', '--depth', '2', '--', 'origin', 'master']
121
+ # git_cmd = :fetch
122
+ # git_cmd_args = ['origin', ref: 'master', depth: '2']
123
+ # assert_command_line(expected_command_line, git_cmd, git_cmd_args)
124
+ #
125
+ # @example Fetch all
126
+ # expected_command_line = ['fetch', '--all']
127
+ # git_cmd = :fetch
128
+ # git_cmd_args = [all: true]
129
+ # assert_command_line(expected_command_line, git_cmd, git_cmd_args)
130
+ #
131
+ # @param expected_command_line [Array<String>] The expected arguments to be sent to Git::Lib#command
132
+ # @param git_cmd [Symbol] the method to be called on the Git::Base object
133
+ # @param git_cmd_args [Array<Object>] The arguments to be sent to the git_cmd method
134
+ #
135
+ # @yield [git] An initialization block
136
+ # The initialization block is called after a test project is created with Git.init.
137
+ # The current working directory is set to the root of the test project's working tree.
138
+ # @yieldparam git [Git::Base] The Git::Base object resulting from initializing the test project
139
+ # @yieldreturn [void] the return value of the block is ignored
140
+ #
141
+ # @return [void]
142
+ #
143
+ def assert_command_line ( expected_command_line , git_cmd , git_cmd_args )
144
+ actual_command_line = nil
145
+
146
+ in_temp_dir do |path |
147
+ git = Git . init ( 'test_project' )
148
+
149
+ Dir . chdir 'test_project' do
150
+ yield ( git ) if block_given?
151
+
152
+ # Mock the Git::Lib#command method to capture the actual command line args
153
+ git . lib . define_singleton_method ( :command ) do |cmd , *opts , &block |
154
+ actual_command_line = [ cmd , *opts . flatten ]
155
+ end
156
+
157
+ git . send ( git_cmd , *git_cmd_args )
158
+ end
159
+ end
160
+
161
+ assert_equal ( expected_command_line , actual_command_line )
162
+ end
100
163
end
0 commit comments