Repository: ovh/cds
Pull request #4901 (2020-01-20 11:46:56)

fix(worker): path.IsAbs is not working on windows
1. Description
1. Related issues
1. About tests
1. Mentions

@ovh/cds


ovh-cds: 
CDS Report build-all-cds#12114.0 ✘  
* Build
  * Build and Package All ✔ 
  * Build Debpacker ✔ 
  * Build OS-Ansible-Inventory ✔ 
  * Build SMTPMock ✔ 
  * Lint UI Translate ✔ 
  * Test CLI ✔ 
  * Test Engine ✔ 
  * Test SDK ✔ 
  * Test UI ✔ 
* Package
  * Docker ✔ 
  * Docs ✔ 
* Integration
  * Docker Compose Tests ✔ 
  * Helm Tests -   
  * Standalone Tests ✘  

ovh-cds: 
CDS Report build-all-cds#12114.1 ✘  
* Build
  * Build and Package All ✔ 
  * Build Debpacker ✔ 
  * Build OS-Ansible-Inventory ✔ 
  * Build SMTPMock ✔ 
  * Lint UI Translate ✔ 
  * Test CLI ✔ 
  * Test Engine ✔ 
  * Test SDK ✔ 
  * Test UI ✔ 
* Package
  * Docker ✔ 
  * Docs ✔ 
* Integration
  * Docker Compose Tests ✔ 
  * Helm Tests -   
  * Standalone Tests ✘  


Patch:
diff --git a/engine/worker/cmd_upload.go b/engine/worker/cmd_upload.go
index ce935f831..f7f7c94f7 100644
--- a/engine/worker/cmd_upload.go
+++ b/engine/worker/cmd_upload.go
@@ -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)
 			}
 		}
 	}
diff --git a/engine/worker/internal/action/builtin_artifact_upload.go b/engine/worker/internal/action/builtin_artifact_upload.go
index 342a67f45..0a5624da3 100644
--- a/engine/worker/internal/action/builtin_artifact_upload.go
+++ b/engine/worker/internal/action/builtin_artifact_upload.go
@@ -4,7 +4,6 @@ import (
 	"context"
 	"errors"
 	"fmt"
-	"path"
 	"path/filepath"
 	"strings"
 	"sync"
@@ -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)
 	}
 
diff --git a/engine/worker/internal/action/builtin_coverage.go b/engine/worker/internal/action/builtin_coverage.go
index 9580f36e7..9e97c5947 100644
--- a/engine/worker/internal/action/builtin_coverage.go
+++ b/engine/worker/internal/action/builtin_coverage.go
@@ -3,7 +3,6 @@ package action
 import (
 	"context"
 	"fmt"
-	"path"
 	"path/filepath"
 	"strconv"
 
@@ -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
diff --git a/engine/worker/internal/action/builtin_junit.go b/engine/worker/internal/action/builtin_junit.go
index 8a461401d..a16277537 100644
--- a/engine/worker/internal/action/builtin_junit.go
+++ b/engine/worker/internal/action/builtin_junit.go
@@ -5,7 +5,6 @@ import (
 	"encoding/xml"
 	"errors"
 	"fmt"
-	"path"
 	"path/filepath"
 
 	"github.com/spf13/afero"
@@ -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)
 	}
 
diff --git a/engine/worker/internal/handler_upload.go b/engine/worker/internal/handler_upload.go
index e4c47435f..8db4e24e9 100644
--- a/engine/worker/internal/handler_upload.go
+++ b/engine/worker/internal/handler_upload.go
@@ -6,7 +6,6 @@ import (
 	"fmt"
 	"io/ioutil"
 	"net/http"
-	"path"
 	"path/filepath"
 
 	"github.com/ovh/cds/engine/worker/internal/action"
@@ -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)
 		}
 
diff --git a/sdk/cache.go b/sdk/cache.go
index 9afaac51e..d9fa52489 100644
--- a/sdk/cache.go
+++ b/sdk/cache.go
@@ -7,7 +7,6 @@ import (
 	"io"
 	"net/url"
 	"os"
-	"path"
 	"path/filepath"
 	"strings"
 
@@ -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)
 		}
 
diff --git a/sdk/common.go b/sdk/common.go
index dfe3fc5da..02f9e31ef 100644
--- a/sdk/common.go
+++ b/sdk/common.go
@@ -11,8 +11,10 @@ import (
 	"fmt"
 	"io"
 	"os"
+	"path"
 	"reflect"
 	"regexp"
+	"runtime"
 	"strings"
 	"unicode"
 
@@ -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)
+}


Test patch:
diff --git a/sdk/common_test.go b/sdk/common_test.go
index 595b67a4a..acd62275e 100644
--- a/sdk/common_test.go
+++ b/sdk/common_test.go
@@ -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`))
+}