diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-04-23 12:18:55 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-04-27 10:53:01 +0300 |
commit | a07df3bd31babdfb75df33bff317bd8a07ea8e76 (patch) | |
tree | 82b0d5dec24d02ce7f8a25989b0de3afb8039c3f | |
parent | 2d3e9b1a92af1529257f878aa0406bf8a4dba7e9 (diff) |
hooks: Fix test not cleaning up hooks
The custom hooks test writes a set of hooks, but does not clean up all
of them. Fix this by converting the tests to be table-driven, allowing
us to use `defer` for cleanup.
-rw-r--r-- | internal/service/hooks/custom_hooks_test.go | 109 |
1 files changed, 56 insertions, 53 deletions
diff --git a/internal/service/hooks/custom_hooks_test.go b/internal/service/hooks/custom_hooks_test.go index 13f784eea..3b43971c8 100644 --- a/internal/service/hooks/custom_hooks_test.go +++ b/internal/service/hooks/custom_hooks_test.go @@ -107,60 +107,63 @@ func TestCustomHookPartialFailure(t *testing.T) { ctx, cancel := testhelper.Context() defer cancel() - // project hook success, global hook failure - projectHookPath := filepath.Join(testRepoPath, "custom_hooks") - cleanupProjectHook := writeCustomHook(t, "pre-receive", projectHookPath, successScript) - globalHookPath := filepath.Join(globalCustomHooksDir, "pre-receive.d") - cleanupGlobalHook := writeCustomHook(t, "pre-receive", globalHookPath, failScript) - - caller, err := newCustomHooksExecutor(testRepoPath, globalCustomHooksDir, "pre-receive") - require.NoError(t, err) - var stdout, stderr bytes.Buffer - - require.Error(t, caller(ctx, nil, nil, &bytes.Buffer{}, &stdout, &stderr)) - - // since global hooks execute after project hooks, the project hook should run and succeed - require.Equal(t, filepath.Join(projectHookPath, "pre-receive"), text.ChompBytes(stdout.Bytes())) - require.Equal(t, filepath.Join(globalHookPath, "pre-receive"), text.ChompBytes(stderr.Bytes())) - - cleanupProjectHook() - cleanupGlobalHook() - - // project hook failure, global hook success - globalHookPath = filepath.Join(globalCustomHooksDir, "post-receive.d") - cleanupProjectHook = writeCustomHook(t, "post-receive", projectHookPath, failScript) - cleanupGlobalHook = writeCustomHook(t, "post-receive", globalHookPath, successScript) - - caller, err = newCustomHooksExecutor(testRepoPath, globalCustomHooksDir, "post-receive") - require.NoError(t, err) - stdout.Reset() - stderr.Reset() - - require.Error(t, caller(ctx, nil, nil, &bytes.Buffer{}, &stdout, &stderr)) - - // since the global hooks execute after project hooks, the global hook should never have run - require.Equal(t, filepath.Join(projectHookPath, "post-receive"), text.ChompBytes(stderr.Bytes())) - require.Equal(t, "", text.ChompBytes(stdout.Bytes())) - - cleanupProjectHook() - cleanupGlobalHook() - - // project hooks failure, global hooks success - globalHookPath = filepath.Join(globalCustomHooksDir, "update.d") - projectHooksPath := filepath.Join(testRepoPath, "custom_hooks", "update.d") - cleanupProjectHook = writeCustomHook(t, "update", projectHooksPath, failScript) - cleanupGlobalHook = writeCustomHook(t, "update", globalHookPath, successScript) - - caller, err = newCustomHooksExecutor(testRepoPath, globalCustomHooksDir, "update") - require.NoError(t, err) - stdout.Reset() - stderr.Reset() - - require.Error(t, caller(ctx, nil, nil, &bytes.Buffer{}, &stdout, &stderr)) + testCases := []struct { + hook string + projectHookSucceeds bool + globalHookSucceeds bool + }{ + { + hook: "pre-receive", + projectHookSucceeds: true, + globalHookSucceeds: false, + }, + { + hook: "post-receive", + projectHookSucceeds: false, + globalHookSucceeds: true, + }, + { + hook: "update", + projectHookSucceeds: false, + globalHookSucceeds: true, + }, + } - // since the global hooks execute after project hooks, the global hook should never have run - require.Equal(t, filepath.Join(projectHooksPath, "update"), text.ChompBytes(stderr.Bytes())) - require.Equal(t, "", text.ChompBytes(stdout.Bytes())) + for _, tc := range testCases { + t.Run(tc.hook, func(t *testing.T) { + projectHookScript := successScript + if !tc.projectHookSucceeds { + projectHookScript = failScript + } + projectHookPath := filepath.Join(testRepoPath, "custom_hooks") + cleanup := writeCustomHook(t, tc.hook, projectHookPath, projectHookScript) + defer cleanup() + + globalHookScript := successScript + if !tc.globalHookSucceeds { + globalHookScript = failScript + } + globalHookPath := filepath.Join(globalCustomHooksDir, fmt.Sprintf("%s.d", tc.hook)) + cleanup = writeCustomHook(t, tc.hook, globalHookPath, globalHookScript) + defer cleanup() + + caller, err := newCustomHooksExecutor(testRepoPath, globalCustomHooksDir, tc.hook) + require.NoError(t, err) + + var stdout, stderr bytes.Buffer + require.Error(t, caller(ctx, nil, nil, &bytes.Buffer{}, &stdout, &stderr)) + + if tc.projectHookSucceeds && tc.globalHookSucceeds { + require.Equal(t, filepath.Join(projectHookPath, tc.hook), text.ChompBytes(stdout.Bytes())) + require.Equal(t, filepath.Join(globalHookPath, tc.hook), text.ChompBytes(stdout.Bytes())) + } else if tc.projectHookSucceeds && !tc.globalHookSucceeds { + require.Equal(t, filepath.Join(projectHookPath, tc.hook), text.ChompBytes(stdout.Bytes())) + require.Equal(t, filepath.Join(globalHookPath, tc.hook), text.ChompBytes(stderr.Bytes())) + } else { + require.Equal(t, filepath.Join(projectHookPath, tc.hook), text.ChompBytes(stderr.Bytes())) + } + }) + } } func TestCustomHooksMultipleHooks(t *testing.T) { |