[task_drivers] Add helper for absolute paths
This is duplicated several times in the Skia repo.
Bug: skia:10812
Change-Id: Ic8110151aaf5ac962c20793ac839d244c5eee380
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/327619
Reviewed-by: Eric Boren <borenet@google.com>
diff --git a/task_driver/go/td/step.go b/task_driver/go/td/step.go
index 8b96175..df39867 100644
--- a/task_driver/go/td/step.go
+++ b/task_driver/go/td/step.go
@@ -8,6 +8,7 @@
"net/http"
"net/url"
"os"
+ "path/filepath"
"sort"
"strings"
"sync"
@@ -16,6 +17,7 @@
multierror "github.com/hashicorp/go-multierror"
"go.skia.org/infra/go/exec"
"go.skia.org/infra/go/httputils"
+ "go.skia.org/infra/go/skerr"
"go.skia.org/infra/go/sklog"
"go.skia.org/infra/go/util"
fsnotify "gopkg.in/fsnotify.v1"
@@ -480,3 +482,16 @@
}
return c
}
+
+// MustGetAbsolutePathOfFlag returns the absolute path to the specified path or exits with an
+// error indicating that the given flag must be specified.
+func MustGetAbsolutePathOfFlag(ctx context.Context, nonEmptyPath, flag string) string {
+ if nonEmptyPath == "" {
+ Fatalf(ctx, "--%s must be specified", flag)
+ }
+ absPath, err := filepath.Abs(nonEmptyPath)
+ if err != nil {
+ Fatal(ctx, skerr.Wrap(err))
+ }
+ return absPath
+}
diff --git a/task_driver/go/td/step_test.go b/task_driver/go/td/step_test.go
index 90a80db..1bfa34c 100644
--- a/task_driver/go/td/step_test.go
+++ b/task_driver/go/td/step_test.go
@@ -8,6 +8,7 @@
"strings"
"testing"
+ "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.skia.org/infra/go/deepequal/assertdeep"
"go.skia.org/infra/go/exec"
@@ -401,3 +402,33 @@
require.NotNil(t, data)
assertdeep.Equal(t, data.Env, expect)
}
+
+func TestMustGetAbsolutePathOfFlag_NonEmptyPath_Success(t *testing.T) {
+ unittest.SmallTest(t)
+ wd, err := os.Getwd()
+ require.NoError(t, err)
+ assert.NotEmpty(t, wd)
+
+ s := RunTestSteps(t, false, func(ctx context.Context) error {
+ path := MustGetAbsolutePathOfFlag(ctx, "my_dir", "some_flag")
+
+ assert.Contains(t, path, wd)
+ assert.Contains(t, path, "my_dir")
+ return nil
+ })
+ assert.Empty(t, s.Errors)
+ assert.Empty(t, s.Exceptions)
+}
+
+func TestMustGetAbsolutePathOfFlag_EmptyPath_Panics(t *testing.T) {
+ unittest.SmallTest(t)
+
+ s := RunTestSteps(t, true, func(ctx context.Context) error {
+ MustGetAbsolutePathOfFlag(ctx, "", "some_flag")
+ assert.Fail(t, "should not reach here")
+ return nil
+ })
+ assert.Empty(t, s.Exceptions)
+ require.Len(t, s.Errors, 1)
+ assert.Contains(t, s.Errors[0], "some_flag must")
+}