github.com/facebookincubator/ttpforge@v1.0.13-0.20240405153150-5ae801628835/pkg/args/path_test.go (about)

     1  /*
     2  Copyright © 2023-present, Meta Platforms, Inc. and affiliates
     3  Permission is hereby granted, free of charge, to any person obtaining a copy
     4  of this software and associated documentation files (the "Software"), to deal
     5  in the Software without restriction, including without limitation the rights
     6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  copies of the Software, and to permit persons to whom the Software is
     8  furnished to do so, subject to the following conditions:
     9  The above copyright notice and this permission notice shall be included in
    10  all copies or substantial portions of the Software.
    11  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    12  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    13  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    14  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    15  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    16  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    17  THE SOFTWARE.
    18  */
    19  
    20  package args
    21  
    22  import (
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestValidateArgsPath(t *testing.T) {
    31  
    32  	// used in some test cases
    33  	homedir, err := os.UserHomeDir()
    34  	require.NoError(t, err)
    35  
    36  	// cd into this directory and use it to verify
    37  	// that relative paths are expanded appropriately
    38  	tmpDir, err := os.MkdirTemp("", "ttpforge-testing")
    39  	require.NoError(t, err)
    40  	defer os.RemoveAll(tmpDir)
    41  	wd, err := os.Getwd()
    42  	require.NoError(t, err)
    43  	err = os.Chdir(tmpDir)
    44  	require.NoError(t, err)
    45  	defer func() {
    46  		// this will break a bunch of other tests so be loud about it
    47  		if err := os.Chdir(wd); err != nil {
    48  			panic(err)
    49  		}
    50  	}()
    51  	// macOS /private/var/...  shenanigans
    52  	tmpDir, err = filepath.EvalSymlinks(tmpDir)
    53  	require.NoError(t, err)
    54  
    55  	testCases := []validateTestCase{
    56  		{
    57  			name: "Absolute Path",
    58  			specs: []Spec{
    59  				{
    60  					Name: "mypath",
    61  					Type: "path",
    62  				},
    63  			},
    64  			argKvStrs: []string{
    65  				"mypath=/tmp/foo",
    66  			},
    67  			expectedResult: map[string]any{
    68  				"mypath": "/tmp/foo",
    69  			},
    70  		},
    71  		{
    72  			name: "Path Containing Tilde",
    73  			specs: []Spec{
    74  				{
    75  					Name: "mypath",
    76  					Type: "path",
    77  				},
    78  			},
    79  			argKvStrs: []string{
    80  				"mypath=~/wut",
    81  			},
    82  			expectedResult: map[string]any{
    83  				"mypath": filepath.Join(homedir, "wut"),
    84  			},
    85  		},
    86  		{
    87  			name: "Relative Path",
    88  			specs: []Spec{
    89  				{
    90  					Name: "mypath",
    91  					Type: "path",
    92  				},
    93  			},
    94  			argKvStrs: []string{
    95  				"mypath=" + filepath.Join("foo", "bar"),
    96  			},
    97  			// can't do a strict equality 0
    98  			expectedResult: map[string]any{
    99  				"mypath": filepath.Join(tmpDir, "foo", "bar"),
   100  			},
   101  		},
   102  	}
   103  
   104  	for _, tc := range testCases {
   105  		t.Run(tc.name, func(t *testing.T) {
   106  			checkValidateTestCase(t, tc)
   107  		})
   108  	}
   109  }