github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/pkg/functions/functions_test.go (about) 1 /* 2 * Copyright 2018 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package functions 18 19 import ( 20 "testing" 21 "github.com/stretchr/testify/assert" 22 "path/filepath" 23 "os" 24 "github.com/projectriff/riff-cli/pkg/osutils" 25 ) 26 27 28 func TestFunctionNameFromPathFromCurrentDirectory(t *testing.T) { 29 currentDir,_ := filepath.Abs(".") 30 os.Chdir(osutils.Path("../../test_data/shell/echo")) 31 32 as := assert.New(t) 33 34 fname, err := FunctionNameFromPath(".") 35 as.NoError(err) 36 37 as.Equal("echo",fname) 38 39 os.Chdir(currentDir) 40 } 41 42 func TestFunctionNameFromRelativePath(t *testing.T) { 43 as := assert.New(t) 44 fname, err := FunctionNameFromPath(osutils.Path("../../test_data/shell/echo")) 45 as.NoError(err) 46 as.Equal("echo",fname) 47 } 48 49 func TestFunctionNameFromRegularFile(t *testing.T) { 50 as := assert.New(t) 51 fname, err := FunctionNameFromPath(osutils.Path("../../test_data/shell/echo/echo.sh")) 52 as.NoError(err) 53 as.Equal("echo",fname) 54 } 55 56 func TestFunctionNameFromInvalidPathIsEmpty(t *testing.T) { 57 as := assert.New(t) 58 fname, err := FunctionNameFromPath("a/b/c/d") 59 as.Error(err) 60 as.Empty(fname) 61 } 62 63 func TestFunctionDirFromRelativePath(t *testing.T) { 64 as := assert.New(t) 65 fndir, err := FunctionDirFromPath(osutils.Path("../../test_data/shell/echo")) 66 as.NoError(err) 67 absEcho, _ := AbsPath("../../test_data/shell/echo") 68 as.Equal(absEcho,fndir) 69 } 70 71 func TestFunctionDirFromRegularFile(t *testing.T) { 72 as := assert.New(t) 73 fndir, err := FunctionDirFromPath(osutils.Path("../../test_data/shell/echo/echo.sh")) 74 as.NoError(err) 75 absEcho, _ := AbsPath("../../test_data/shell/echo") 76 as.Equal(absEcho,fndir) 77 } 78 79 80