Skip to content

Commit 38d6a83

Browse files
committed
test(auth status): correctly replace JSON-escaped paths
Signed-off-by: Babak K. Shandiz <babakks@github.com>
1 parent e31136a commit 38d6a83

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

pkg/cmd/auth/status/status_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package status
33
import (
44
"bytes"
55
"context"
6+
"encoding/json"
67
"net/http"
78
"path/filepath"
89
"strings"
@@ -750,8 +751,9 @@ func Test_statusRun(t *testing.T) {
750751
} else {
751752
require.NoError(t, err)
752753
}
753-
output := strings.ReplaceAll(stdout.String(), config.ConfigDir()+string(filepath.Separator), "GH_CONFIG_DIR/")
754-
errorOutput := strings.ReplaceAll(stderr.String(), config.ConfigDir()+string(filepath.Separator), "GH_CONFIG_DIR/")
754+
755+
output := replaceAll(stdout.String(), config.ConfigDir()+string(filepath.Separator), "GH_CONFIG_DIR/")
756+
errorOutput := replaceAll(stderr.String(), config.ConfigDir()+string(filepath.Separator), "GH_CONFIG_DIR/")
755757

756758
require.Equal(t, tt.wantErrOut, errorOutput)
757759
require.Equal(t, tt.wantOut, output)
@@ -764,3 +766,19 @@ func login(t *testing.T, c gh.Config, hostname, username, token, protocol string
764766
_, err := c.Authentication().Login(hostname, username, token, protocol, false)
765767
require.NoError(t, err)
766768
}
769+
770+
// replaceAll replaces all instances of old with new in s, as well as all instances
771+
// of the JSON-escaped version of old with the JSON-escaped version of new.
772+
// This is because when the test is run on Windows the paths will have backslashes
773+
// escaped in JSON and a simple strings.ReplaceAll won't catch them.
774+
func replaceAll(s string, old string, new string) string {
775+
jsonEscapedOld, _ := json.Marshal(old)
776+
jsonEscapedOld = jsonEscapedOld[1 : len(jsonEscapedOld)-1]
777+
778+
jsonEscapedNew, _ := json.Marshal(new)
779+
jsonEscapedNew = jsonEscapedNew[1 : len(jsonEscapedNew)-1]
780+
781+
replaced := strings.ReplaceAll(s, string(jsonEscapedOld), string(jsonEscapedNew))
782+
replaced = strings.ReplaceAll(replaced, old, new)
783+
return replaced
784+
}

0 commit comments

Comments
 (0)