Skip to content

Commit

Permalink
fix: -path and -file bug (#766)
Browse files Browse the repository at this point in the history
* fix: -path and -file bug

Signed-off-by: Keming <[email protected]>

* add test case

Signed-off-by: Keming <[email protected]>

Signed-off-by: Keming <[email protected]>
  • Loading branch information
kemingy authored Aug 12, 2022
1 parent 4a359f1 commit b9f0af8
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/app/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/util/fileutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
82 changes: 82 additions & 0 deletions pkg/util/fileutil/file_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit b9f0af8

Please sign in to comment.