From b9f0af8056129fe6c5a2e590cd428463186cac0e Mon Sep 17 00:00:00 2001 From: Keming Date: Fri, 12 Aug 2022 21:05:13 +0800 Subject: [PATCH] fix: -path and -file bug (#766) * fix: -path and -file bug Signed-off-by: Keming * add test case Signed-off-by: Keming Signed-off-by: Keming --- pkg/app/build.go | 3 +- pkg/app/run.go | 3 +- pkg/util/fileutil/file.go | 26 +++++++++++ pkg/util/fileutil/file_test.go | 82 ++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 pkg/util/fileutil/file_test.go diff --git a/pkg/app/build.go b/pkg/app/build.go index 650a18c7c..fd1526cb8 100644 --- a/pkg/app/build.go +++ b/pkg/app/build.go @@ -26,6 +26,7 @@ import ( "github.com/tensorchord/envd/pkg/docker" "github.com/tensorchord/envd/pkg/home" sshconfig "github.com/tensorchord/envd/pkg/ssh/config" + "github.com/tensorchord/envd/pkg/util/fileutil" ) var CommandBuild = &cli.Command{ @@ -149,7 +150,7 @@ func ParseBuildOpt(clicontext *cli.Context) (builder.Options, error) { return builder.Options{}, err } - manifest, err := filepath.Abs(filepath.Join(buildContext, fileName)) + manifest, err := fileutil.FindFileAbsPath(buildContext, fileName) if err != nil { return builder.Options{}, errors.Wrap(err, "failed to get absolute path of the build file") } diff --git a/pkg/app/run.go b/pkg/app/run.go index 0c58f46cd..52e60f817 100644 --- a/pkg/app/run.go +++ b/pkg/app/run.go @@ -25,6 +25,7 @@ import ( "github.com/tensorchord/envd/pkg/docker" "github.com/tensorchord/envd/pkg/lang/ir" "github.com/tensorchord/envd/pkg/ssh" + "github.com/tensorchord/envd/pkg/util/fileutil" ) var CommandRun = &cli.Command{ @@ -84,7 +85,7 @@ func run(clicontext *cli.Context) error { if err != nil { return err } - manifest, err := filepath.Abs(filepath.Join(buildContext, fileName)) + manifest, err := fileutil.FindFileAbsPath(buildContext, fileName) if err != nil { return errors.Wrap(err, "failed to get absolute path of the build file") } diff --git a/pkg/util/fileutil/file.go b/pkg/util/fileutil/file.go index a225d9f58..1ece58a2d 100644 --- a/pkg/util/fileutil/file.go +++ b/pkg/util/fileutil/file.go @@ -51,6 +51,32 @@ func FileExists(filename string) (bool, error) { return !info.IsDir(), nil } +// FindFileAbsPath returns the absolute path for the given path and file +func FindFileAbsPath(path, fileName string) (string, error) { + if len(fileName) <= 0 { + return "", errors.New("file name is empty") + } + manifest := filepath.Join(path, fileName) + exist, err := FileExists(manifest) + if err != nil { + return "", err + } + var absPath string + if exist { + absPath, err = filepath.Abs(manifest) + if err != nil { + return "", err + } + return absPath, nil + } + // check if ${PWD}/fileName exists + absPath, err = filepath.Abs(fileName) + if err != nil { + return "", err + } + return absPath, nil +} + func RemoveAll(dirname string) error { return os.RemoveAll(dirname) } diff --git a/pkg/util/fileutil/file_test.go b/pkg/util/fileutil/file_test.go new file mode 100644 index 000000000..72ab5415a --- /dev/null +++ b/pkg/util/fileutil/file_test.go @@ -0,0 +1,82 @@ +// Copyright 2022 The envd Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package fileutil + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestFindFileAbsPath(t *testing.T) { + type testCase struct { + name string + filePath string + fileName string + expectPath string + expectError bool + } + + dir, err := os.MkdirTemp("", "envd-test") + require.Nil(t, err, "create temp dir failed") + defer os.RemoveAll(dir) + name := "tmpfile" + err = os.WriteFile(filepath.Join(dir, name), []byte("test"), 0666) + require.Nil(t, err, "write temp file failed") + expect, err := filepath.Abs(filepath.Join(dir, name)) + require.Nil(t, err, "cannot get the abs path") + + testCases := []testCase{ + { + "path + file with path", + dir, + name, + expect, + false, + }, + { + "empth path + full file", + "", + expect, + expect, + false, + }, + { + "empty file name", + dir, + "", + "", + true, + }, + { + "path + full file", + dir, + expect, + expect, + false, + }, + } + + for _, tc := range testCases { + res, err := FindFileAbsPath(tc.filePath, tc.fileName) + if tc.expectError { + require.Error(t, err) + } else { + require.Equal(t, res, tc.expectPath, tc.name) + } + } +}