Skip to content

Commit

Permalink
fix(worker): path.IsAbs is not working on windows (#4901)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin authored and sguiheux committed Jan 20, 2020
1 parent f561705 commit 527be9d
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion engine/worker/cmd_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func uploadCmd() func(cmd *cobra.Command, args []string) {
}

if resp.StatusCode >= 300 {
sdk.Exit("cannot artefact upload HTTP %d\n", resp.StatusCode)
sdk.Exit("cannot artifact upload HTTP %d\n", resp.StatusCode)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions engine/worker/internal/action/builtin_artifact_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"path"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -42,7 +41,7 @@ func RunArtifactUpload(ctx context.Context, wk workerruntime.Runtime, a sdk.Acti
abs = workdir.Name()
}

if !path.IsAbs(artifactPath) {
if !sdk.PathIsAbs(artifactPath) {
artifactPath = filepath.Join(abs, artifactPath)
}

Expand Down
3 changes: 1 addition & 2 deletions engine/worker/internal/action/builtin_coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package action
import (
"context"
"fmt"
"path"
"path/filepath"
"strconv"

Expand Down Expand Up @@ -64,7 +63,7 @@ func RunParseCoverageResultAction(ctx context.Context, wk workerruntime.Runtime,
abs = workdir.Name()
}

if !path.IsAbs(p) {
if !sdk.PathIsAbs(p) {
fpath = filepath.Join(abs, p)
} else {
fpath = p
Expand Down
3 changes: 1 addition & 2 deletions engine/worker/internal/action/builtin_junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/xml"
"errors"
"fmt"
"path"
"path/filepath"

"github.com/spf13/afero"
Expand Down Expand Up @@ -43,7 +42,7 @@ func RunParseJunitTestResultAction(ctx context.Context, wk workerruntime.Runtime
abs = workdir.Name()
}

if !path.IsAbs(p) {
if !sdk.PathIsAbs(p) {
p = filepath.Join(abs, p)
}

Expand Down
3 changes: 1 addition & 2 deletions engine/worker/internal/handler_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"path"
"path/filepath"

"github.com/ovh/cds/engine/worker/internal/action"
Expand All @@ -31,7 +30,7 @@ func uploadHandler(ctx context.Context, wk *CurrentWorker) http.HandlerFunc {
}

artifactPath := art.Name
if !path.IsAbs(artifactPath) {
if !sdk.PathIsAbs(artifactPath) {
artifactPath = filepath.Join(art.WorkingDirectory, art.Name)
}

Expand Down
3 changes: 1 addition & 2 deletions sdk/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"net/url"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -58,7 +57,7 @@ func CreateTarFromPaths(fs afero.Fs, cwd string, paths []string, opts *TarOption
// ensure the src actually exists before trying to tar it

completePath := p
if !path.IsAbs(p) {
if !PathIsAbs(p) {
completePath = filepath.Join(cwd, p)
}

Expand Down
11 changes: 11 additions & 0 deletions sdk/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"fmt"
"io"
"os"
"path"
"reflect"
"regexp"
"runtime"
"strings"
"unicode"

Expand Down Expand Up @@ -269,3 +271,12 @@ func RemoveNotPrintableChar(in string) string {
}
return strings.Map(m, in)
}

var windowsPathRegex = regexp.MustCompile(`^[a-zA-Z]:\\[\\\S|*\S]?.*$`)

func PathIsAbs(s string) bool {
if GOOS == "windows" || runtime.GOOS == "windows" {
return windowsPathRegex.MatchString(s)
}
return path.IsAbs(s)
}
9 changes: 9 additions & 0 deletions sdk/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ func TestRemoveNotPrintableChar(t *testing.T) {
})
}
}

func TestPathIsAbs(t *testing.T) {
GOOS = "windows"
assert.True(t, PathIsAbs(`C:\Program Files (x86)\Foo`))
assert.False(t, PathIsAbs(`Program Files (x86)\Foo`))
GOOS = "linux"
assert.True(t, PathIsAbs(`/tmp`))
assert.False(t, PathIsAbs(`tmp`))
}

0 comments on commit 527be9d

Please sign in to comment.