fix: testing for flag files on windows (#1249)

* fix: testing for flag files on windows
* fix build script on windows/msys
pull/1277/head
nils måsén 2 years ago committed by GitHub
parent 2f4d58776d
commit 56368a7207
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,9 @@
#!/bin/bash #!/bin/bash
BINFILE=watchtower
if [ -n "$MSYSTEM" ]; then
BINFILE=watchtower.exe
fi
VERSION=$(git describe --tags) VERSION=$(git describe --tags)
echo "Building $VERSION..." echo "Building $VERSION..."
go build -o watchtower -ldflags "-X github.com/containrrr/watchtower/internal/meta.Version=$VERSION" go build -o $BINFILE -ldflags "-X github.com/containrrr/watchtower/internal/meta.Version=$VERSION"

@ -1,6 +1,7 @@
package flags package flags
import ( import (
"errors"
"io/ioutil" "io/ioutil"
"os" "os"
"strings" "strings"
@ -468,9 +469,13 @@ func getSecretFromFile(flags *pflag.FlagSet, secret string) {
} }
func isFile(s string) bool { func isFile(s string) bool {
_, err := os.Stat(s) firstColon := strings.IndexRune(s, ':')
if os.IsNotExist(err) { if firstColon != 1 && firstColon != -1 {
// If the string contains a ':', but it's not the second character, it's probably not a file
// and will cause a fatal error on windows if stat'ed
// This still allows for paths that start with 'c:\' etc.
return false return false
} }
return true _, err := os.Stat(s)
return !errors.Is(err, os.ErrNotExist)
} }

@ -11,6 +11,10 @@ import (
) )
func TestEnvConfig_Defaults(t *testing.T) { func TestEnvConfig_Defaults(t *testing.T) {
// Unset testing environments own variables, since those are not what is under test
os.Unsetenv("DOCKER_TLS_VERIFY")
os.Unsetenv("DOCKER_HOST")
cmd := new(cobra.Command) cmd := new(cobra.Command)
SetDefaults() SetDefaults()
RegisterDockerFlags(cmd) RegisterDockerFlags(cmd)
@ -94,3 +98,8 @@ func TestHTTPAPIPeriodicPollsFlag(t *testing.T) {
assert.Equal(t, true, periodicPolls) assert.Equal(t, true, periodicPolls)
} }
func TestIsFile(t *testing.T) {
assert.False(t, isFile("https://google.com"), "an URL should never be considered a file")
assert.True(t, isFile(os.Args[0]), "the currently running binary path should always be considered a file")
}

Loading…
Cancel
Save