github.com/nuvolaris/nuv@v0.0.0-20240511174247-a74e3a52bfd8/plugin_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  
    18  package main
    19  
    20  import (
    21  	"io"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func copyFile(srcPath, destPath string) error {
    30  	srcFile, err := os.Open(srcPath)
    31  	if err != nil {
    32  		return err
    33  	}
    34  	defer srcFile.Close()
    35  
    36  	destFile, err := os.Create(destPath)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	defer destFile.Close()
    41  
    42  	_, err = io.Copy(destFile, srcFile)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	return nil
    48  }
    49  func setupPluginTest(dir string, t *testing.T) string {
    50  	t.Helper()
    51  	// create the olaris-test folder
    52  	olarisTestDir := filepath.Join(dir, "olaris-test")
    53  	err := os.MkdirAll(olarisTestDir, 0755)
    54  	require.NoError(t, err)
    55  
    56  	// copy the nuvroot.json from tests/olaris into the olaris-test folder
    57  	nuvRootJSON := filepath.Join("tests", "olaris", "nuvroot.json")
    58  	err = copyFile(nuvRootJSON, filepath.Join(olarisTestDir, "nuvroot.json"))
    59  	require.NoError(t, err)
    60  
    61  	// copy nuvfile.yml from tests/olaris into the olaris-test folder
    62  	nuvfileYML := filepath.Join("tests", "olaris", "nuvfile.yml")
    63  	err = copyFile(nuvfileYML, filepath.Join(olarisTestDir, "nuvfile.yml"))
    64  	require.NoError(t, err)
    65  
    66  	return olarisTestDir
    67  }
    68  
    69  func TestGetAllNuvRootPlugins(t *testing.T) {
    70  	t.Run("success: get all the nuvroots.json from plugins with 1 plugin", func(t *testing.T) {
    71  		tempDir := t.TempDir()
    72  		plgFolder := setupPluginTest(tempDir, t)
    73  		os.Setenv("NUV_ROOT_PLUGIN", tempDir)
    74  
    75  		nuvRoots, err := GetNuvRootPlugins()
    76  		require.NoError(t, err)
    77  		require.Len(t, nuvRoots, 1)
    78  		require.Equal(t, joinpath(plgFolder, NUVROOT), nuvRoots[getPluginName(plgFolder)])
    79  	})
    80  
    81  	t.Run("success: get all the nuvroots.json from plugins with 2 plugins", func(t *testing.T) {
    82  		tempDir := t.TempDir()
    83  		os.Setenv("NUV_ROOT_PLUGIN", tempDir)
    84  		plgFolder := setupPluginTest(tempDir, t)
    85  
    86  		// create the olaris-test2 folder
    87  		olarisTestDir := filepath.Join(tempDir, "olaris-test2")
    88  		err := os.MkdirAll(olarisTestDir, 0755)
    89  		require.NoError(t, err)
    90  
    91  		// copy the nuvroot.json from tests/olaris into the olaris-test folder
    92  		nuvRootJSON := filepath.Join("tests", "olaris", "nuvroot.json")
    93  		err = copyFile(nuvRootJSON, filepath.Join(olarisTestDir, "nuvroot.json"))
    94  		require.NoError(t, err)
    95  
    96  		// copy nuvfile.yml from tests/olaris into the olaris-test folder
    97  		nuvfileYML := filepath.Join("tests", "olaris", "nuvfile.yml")
    98  		err = copyFile(nuvfileYML, filepath.Join(olarisTestDir, "nuvfile.yml"))
    99  		require.NoError(t, err)
   100  
   101  		nuvRoots, err := GetNuvRootPlugins()
   102  		require.NoError(t, err)
   103  		require.Len(t, nuvRoots, 2)
   104  		require.Equal(t, joinpath(plgFolder, NUVROOT), nuvRoots[getPluginName(plgFolder)])
   105  		require.Equal(t, joinpath(olarisTestDir, NUVROOT), nuvRoots[getPluginName(olarisTestDir)])
   106  	})
   107  
   108  	t.Run("empty: no plugins folder found (olaris-*)", func(t *testing.T) {
   109  		tempDir := t.TempDir()
   110  		os.Setenv("NUV_ROOT_PLUGIN", tempDir)
   111  
   112  		// Test when the folder is not found
   113  		nuvRoots, err := GetNuvRootPlugins()
   114  		require.NoError(t, err)
   115  		require.Empty(t, nuvRoots)
   116  	})
   117  }
   118  
   119  func TestFindPluginTask(t *testing.T) {
   120  	t.Run("success: plugin task found in ./olaris-test", func(t *testing.T) {
   121  		tempDir := t.TempDir()
   122  		os.Setenv("NUV_ROOT_PLUGIN", tempDir)
   123  		plgFolder := setupPluginTest(tempDir, t)
   124  
   125  		fld, err := findTaskInPlugins("test")
   126  		require.NoError(t, err)
   127  		require.Equal(t, plgFolder, fld)
   128  	})
   129  
   130  	t.Run("error: no plugins folder found (olaris-*)", func(t *testing.T) {
   131  		tempDir := t.TempDir()
   132  		os.Setenv("NUV_ROOT_PLUGIN", tempDir)
   133  
   134  		// Test when the folder is not found
   135  		fld, err := findTaskInPlugins("grep")
   136  		require.Error(t, err)
   137  		require.Empty(t, fld)
   138  	})
   139  }
   140  
   141  func TestNewPlugins(t *testing.T) {
   142  	t.Run("create plugins struct with valid local dir", func(t *testing.T) {
   143  		tempDir := t.TempDir()
   144  		plgFolder := setupPluginTest(tempDir, t)
   145  
   146  		os.Setenv("NUV_ROOT_PLUGIN", tempDir)
   147  
   148  		p, err := newPlugins()
   149  		require.NoError(t, err)
   150  		require.NotNil(t, p)
   151  		require.Len(t, p.local, 1)
   152  		require.Equal(t, plgFolder, p.local[0])
   153  	})
   154  
   155  	t.Run("non existent local dir results in empty local field", func(t *testing.T) {
   156  		localDir := "/path/to/nonexistent/dir"
   157  		os.Setenv("NUV_ROOT_PLUGIN", localDir)
   158  		p, err := newPlugins()
   159  		require.NoError(t, err)
   160  		require.NotNil(t, p)
   161  		require.Len(t, p.local, 0)
   162  	})
   163  }
   164  
   165  func Example_pluginsPrint() {
   166  	p := plugins{
   167  		local: make([]string, 0),
   168  		nuv:   make([]string, 0),
   169  	}
   170  	p.print()
   171  	// Output
   172  	// No plugins installed. Use 'nuv -plugin' to add new ones.
   173  }
   174  
   175  func TestCheckGitRepo(t *testing.T) {
   176  	tests := []struct {
   177  		url          string
   178  		expectedRepo bool
   179  		expectedName string
   180  	}{
   181  		{
   182  			url:          "https://github.com/giusdp/olaris-test",
   183  			expectedRepo: true,
   184  			expectedName: "olaris-test",
   185  		},
   186  		{
   187  			url:          "https://github.com/giusdp/olaris-test.git",
   188  			expectedRepo: true,
   189  			expectedName: "olaris-test",
   190  		},
   191  		{
   192  			url:          "https://github.com/giusdp/some-repo",
   193  			expectedRepo: false,
   194  			expectedName: "",
   195  		},
   196  		{
   197  			url:          "https://github.com/giusdp/olaris-repo.git",
   198  			expectedRepo: true,
   199  			expectedName: "olaris-repo",
   200  		},
   201  		{
   202  			url:          "https://github.com/olaris-1234/repo",
   203  			expectedRepo: false,
   204  			expectedName: "",
   205  		},
   206  		{
   207  			url:          "https://github.com/giusdp/another-repo.git",
   208  			expectedRepo: false,
   209  			expectedName: "",
   210  		},
   211  	}
   212  
   213  	for _, test := range tests {
   214  		isOlarisRepo, repoName := checkGitRepo(test.url)
   215  		require.Equal(t, test.expectedName, repoName)
   216  		require.Equal(t, test.expectedRepo, isOlarisRepo)
   217  	}
   218  }
   219  
   220  func Test_getPluginName(t *testing.T) {
   221  
   222  	testCases := []struct {
   223  		name     string
   224  		expected string
   225  	}{
   226  		{
   227  			name:     "olaris-test",
   228  			expected: "test",
   229  		},
   230  		{
   231  			name:     "olaris-test-123",
   232  			expected: "test-123",
   233  		},
   234  		{
   235  			name:     "test",
   236  			expected: "test",
   237  		},
   238  		{
   239  			name:     "a/fake/path/to/olaris-test",
   240  			expected: "test",
   241  		},
   242  	}
   243  
   244  	for _, tc := range testCases {
   245  		t.Run(tc.name, func(t *testing.T) {
   246  			name := getPluginName(tc.name)
   247  			require.Equal(t, tc.expected, name)
   248  		})
   249  	}
   250  }