github.com/nuvolaris/nuv@v0.0.0-20240511174247-a74e3a52bfd8/main_test.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one
     2  // or more contributor license agreements.  See the NOTICE file
     3  // distributed with this work for additional information
     4  // regarding copyright ownership.  The ASF licenses this file
     5  // to you under the Apache License, Version 2.0 (the
     6  // "License"); you may not use this file except in compliance
     7  // with the License.  You may obtain a copy of the License at
     8  //
     9  //	http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/mitchellh/go-homedir"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  /// test utils
    31  
    32  // pr print args
    33  func pr(args ...any) {
    34  	fmt.Println(args...)
    35  }
    36  
    37  // as creates a string array
    38  // func as(s ...string) []string {
    39  // 	return s
    40  // }
    41  
    42  var homeDir = ""
    43  var workDir = ""
    44  
    45  // normalize path replacing the variable path with /work
    46  func npath(dir string) string {
    47  	return strings.Replace(dir, workDir, "/work", -1)
    48  }
    49  
    50  // normalize path replaching the variable home part with /home
    51  func nhpath(dir string) string {
    52  	//fmt.Println("dir", dir, "home", homeDir)
    53  	return strings.Replace(dir, homeDir, "/home", -1)
    54  }
    55  
    56  func TestMain(m *testing.M) {
    57  	wd, _ := os.Getwd()
    58  	workDir, _ = filepath.Abs(wd)
    59  	homeDir, _ = homedir.Dir()
    60  	taskDryRun = true
    61  	//debugging = true
    62  	//tracing = true
    63  	os.Exit(m.Run())
    64  }
    65  
    66  func TestSetupNuvRootPlugin(t *testing.T) {
    67  	// Test case 1: NUV_ROOT_PLUGIN is not set
    68  	os.Unsetenv("NUV_ROOT_PLUGIN")
    69  	os.Setenv("NUV_PWD", "/path/to/nuv")
    70  	setNuvRootPluginEnv()
    71  	if os.Getenv("NUV_ROOT_PLUGIN") != "/path/to/nuv" {
    72  		t.Errorf("NUV_ROOT_PLUGIN not set correctly, expected /path/to/nuv but got %s", os.Getenv("NUV_ROOT_PLUGIN"))
    73  	}
    74  
    75  	// Test case 2: NUV_ROOT_PLUGIN is already set
    76  	os.Setenv("NUV_ROOT_PLUGIN", "/path/to/nuv/root")
    77  	setNuvRootPluginEnv()
    78  	if os.Getenv("NUV_ROOT_PLUGIN") != "/path/to/nuv/root" {
    79  		t.Errorf("NUV_ROOT_PLUGIN not set correctly, expected /path/to/nuv/root but got %s", os.Getenv("NUV_ROOT_PLUGIN"))
    80  	}
    81  }
    82  
    83  func TestParseInvokeArgs(t *testing.T) {
    84  	t.Run("Test case 1: No arguments with \"=\"", func(t *testing.T) {
    85  		input1 := []string{}
    86  		expected1 := []string{}
    87  		output1 := parseInvokeArgs(input1)
    88  		require.Equal(t, expected1, output1)
    89  	})
    90  
    91  	t.Run("Test case 2: Single argument with \"=\"", func(t *testing.T) {
    92  		input2 := []string{"key=value"}
    93  		expected2 := []string{"-p", "key", "value"}
    94  		output2 := parseInvokeArgs(input2)
    95  		require.Equal(t, expected2, output2)
    96  	})
    97  
    98  	t.Run("Test case 3: Multiple arguments with \"=\"", func(t *testing.T) {
    99  
   100  		input3 := []string{"key1=value1", "key2=value2", "key3=value3"}
   101  		expected3 := []string{"-p", "key1", "value1", "-p", "key2", "value2", "-p", "key3", "value3"}
   102  		output3 := parseInvokeArgs(input3)
   103  		require.Equal(t, expected3, output3)
   104  	})
   105  
   106  	t.Run("Test case 4: Mixed arguments with \"=\" and without \"=\"", func(t *testing.T) {
   107  		input4 := []string{"key1=value1", "-p", "key2", "value2", "key3=value3"}
   108  		expected4 := []string{"-p", "key1", "value1", "-p", "key2", "value2", "-p", "key3", "value3"}
   109  		output4 := parseInvokeArgs(input4)
   110  		require.Equal(t, expected4, output4)
   111  	})
   112  }