Skip to content

Commit

Permalink
fix: testing for flag files on windows (#1249)
Browse files Browse the repository at this point in the history
* fix: testing for flag files on windows
* fix build script on windows/msys
  • Loading branch information
piksel committed Apr 18, 2022
1 parent 2f4d587 commit 56368a7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 8 additions & 3 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flags

import (
"errors"
"io/ioutil"
"os"
"strings"
Expand Down Expand Up @@ -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)
}
9 changes: 9 additions & 0 deletions internal/flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}

0 comments on commit 56368a7

Please sign in to comment.