From 8aac0984795a28980d91a9f7f71c55845d5f270c Mon Sep 17 00:00:00 2001 From: Matt Moore Date: Tue, 24 Aug 2021 14:20:16 -0700 Subject: [PATCH] Improve the logging in pkg/git testing Currently the "fetch" testing nicely captures logs (to examine output), but doesn't actually surface them on failures. This change adds a `defer`red function that stream the output to `t.Log` so that `git` failures can be debugged. For example, when I introduce a typo into the "commit" command, it now prints: ``` === RUN TestFetch/test-clone-with-sparse-checkout git_test.go:299: exit status 1 git_test.go:216: ["error" git]: Error running git [codmmit --allow-empty -m Hello Moto]: exit status 1 git: 'codmmit' is not a git command. See 'git --help'. The most similar command is commit ``` Previously, this would only print the exit status, which isn't particularly useful. --- pkg/git/git_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/git/git_test.go b/pkg/git/git_test.go index 9b7ce3c51bd..349de0e3a59 100644 --- a/pkg/git/git_test.go +++ b/pkg/git/git_test.go @@ -211,6 +211,11 @@ func TestFetch(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { observer, log := observer.New(zap.InfoLevel) + defer func() { + for _, line := range log.TakeAll() { + t.Logf("[%q git]: %s", line.Level, line.Message) + } + }() logger := zap.New(observer).Sugar() gitDir, cleanup := createTempDir(t) @@ -248,11 +253,11 @@ func TestFetch(t *testing.T) { } if tt.logMessage != "" { - takeAll := log.TakeAll() - if len(takeAll) == 0 { + allLogLines := log.All() + if len(allLogLines) == 0 { t.Fatal("We didn't receive any logging") } - gotmsg := takeAll[0].Message + gotmsg := allLogLines[0].Message if !strings.Contains(gotmsg, tt.logMessage) { t.Errorf("log message: '%s'\n should contains: '%s'", tt.logMessage, gotmsg) }