Skip to content

Commit

Permalink
test: unit tests for utils.go file in lib package (#1012)
Browse files Browse the repository at this point in the history
Signed-off-by: tarun8718 <[email protected]>
  • Loading branch information
tarun8718 authored Apr 5, 2023
1 parent bed16f4 commit 6e92f3a
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions lib/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright IBM Corporation 2023
*
* 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 lib

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/konveyor/move2kube/common"
)

func TestLibUtils(t *testing.T) {

t.Run("test for copying customizations assets data", func(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "tempBaseDir")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
customizationsPath := filepath.Join(tmpDir, "customizations")
err = os.Mkdir(customizationsPath, 0755)
if err != nil {
t.Fatalf("failed to create customizations dir: %v", err)
}

err = CopyCustomizationsAssetsData("")
if err != nil {
t.Errorf("failed to check and copy customizations. Error : %v", err)
}

err = CopyCustomizationsAssetsData("invalid")
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("Expected error %v, but got: %v", os.ErrNotExist, err)
}

err = CopyCustomizationsAssetsData(customizationsPath)
if err != nil {
t.Errorf("failed to check and copy customizations. Error : %v", err)
}
assetsPath, err := filepath.Abs(common.AssetsPath)
if err != nil {
t.Errorf("failed to make the assets path '%s' absolute. Error: %v", assetsPath, err)
}
customizationsAssetsPath := filepath.Join(assetsPath, "custom")
if _, err := os.Stat(customizationsAssetsPath); os.IsNotExist(err) {
t.Errorf("failed as customizations assets directory does not exist. Error : %v", err)
}
})

t.Run("test for check and copy customizations", func(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "tempBaseDir")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
customizationsPath := filepath.Join(tmpDir, "customizations")
err = os.Mkdir(customizationsPath, 0755)
if err != nil {
t.Fatalf("failed to create customizations dir: %v", err)
}

err = CheckAndCopyCustomizations("")
if err != nil {
t.Errorf("failed to check and copy customizations. Error : %v", err)
}

err = CheckAndCopyCustomizations("invalid")
if !errors.Is(err, os.ErrNotExist) {
t.Errorf("Expected error %v, but got: %v", os.ErrNotExist, err)
}

err = CheckAndCopyCustomizations(customizationsPath)
if err != nil {
t.Errorf("failed to check and copy customizations. Error : %v", err)
}
assetsPath, err := filepath.Abs(common.AssetsPath)
if err != nil {
t.Errorf("failed to make the assets path '%s' absolute. Error: %v", assetsPath, err)
}
customizationsAssetsPath := filepath.Join(assetsPath, "custom")
if _, err := os.Stat(customizationsAssetsPath); os.IsNotExist(err) {
t.Errorf("failed as customizations assets directory does not exist. Error : %v", err)
}
})

}

0 comments on commit 6e92f3a

Please sign in to comment.