Skip to content

Commit

Permalink
Fix Workdir error when --filepath flag is used. (#6247)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenma authored Jul 20, 2021
1 parent 823b49f commit a9c1138
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/skaffold/runner/runcontext/v2/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v2
import (
"fmt"
"os"
"path/filepath"

"github.com/google/uuid"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -212,10 +213,14 @@ func GetRunContext(opts config.SkaffoldOptions, configs []*latestV2.SkaffoldConf
kubeContext := kubeConfig.CurrentContext
logrus.Infof("Using kubectl context: %s", kubeContext)

// TODO(dgageot): this should be the folder containing skaffold.yaml. Should also be moved elsewhere.
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("finding current directory: %w", err)
var cwd string
if opts.ConfigurationFile == "" {
cwd, err = os.Getwd()
if err != nil {
return nil, fmt.Errorf("finding current directory: %w", err)
}
} else {
cwd = filepath.Dir(opts.ConfigurationFile)
}

namespaces, err := runnerutil.GetAllPodNamespaces(opts.Namespace, pipelines)
Expand Down
28 changes: 28 additions & 0 deletions pkg/skaffold/runner/runcontext/v2/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ limitations under the License.
package v2

import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
latestV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v2"
"github.com/GoogleContainerTools/skaffold/testutil"
)

Expand Down Expand Up @@ -89,3 +95,25 @@ func TestRunContext_UpdateNamespaces(t *testing.T) {
})
}
}

func TestGetRunContextDefaultWorkdir(t *testing.T) {
testutil.Run(t, "default workdir", func(t *testutil.T) {
rctx, err := GetRunContext(config.SkaffoldOptions{}, []*latestV2.SkaffoldConfig{})
pwd, _ := os.Getwd()
t.CheckDeepEqual(pwd, rctx.WorkingDir)
t.CheckNoError(err)
})
}

func TestGetRunContextCustomWorkdir(t *testing.T) {
testutil.Run(t, "default workdir", func(t *testutil.T) {
tmpDir := t.NewTempDir()
tmpDir.Write("skaffold.yaml", fmt.Sprintf("apiVersion: %s\nkind: Config", latestV1.Version)).
Chdir()
rctx, err := GetRunContext(config.SkaffoldOptions{
ConfigurationFile: filepath.Join(tmpDir.Root(), "skaffold.yaml"),
}, []*latestV2.SkaffoldConfig{})
t.CheckDeepEqual(tmpDir.Root(), rctx.WorkingDir)
t.CheckNoError(err)
})
}

0 comments on commit a9c1138

Please sign in to comment.