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 3 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
BINFILE=watchtower
if [ -n "$MSYSTEM" ]; then
BINFILE=watchtower.exe
fi
VERSION=$(git describe --tags)
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
import (
"errors"
"io/ioutil"
"os"
"strings"
@ -468,9 +469,13 @@ func getSecretFromFile(flags *pflag.FlagSet, secret string) {
}
func isFile(s string) bool {
_, err := os.Stat(s)
if os.IsNotExist(err) {
firstColon := strings.IndexRune(s, ':')
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 true
_, err := os.Stat(s)
return !errors.Is(err, os.ErrNotExist)
}

@ -11,6 +11,10 @@ import (
)
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)
SetDefaults()
RegisterDockerFlags(cmd)
@ -94,3 +98,8 @@ func TestHTTPAPIPeriodicPollsFlag(t *testing.T) {
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