github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/filelocation/os_file_test.go (about)

     1  package filelocation
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/datawire/dlib/dlog"
    13  )
    14  
    15  func TestUser(t *testing.T) {
    16  	type testcase struct {
    17  		InputGOOS string
    18  		InputHOME string
    19  		InputEnv  map[string]string
    20  
    21  		ExpectedHomeDir   string
    22  		ExpectedCacheDir  string
    23  		ExpectedConfigDir string
    24  	}
    25  	testcases := map[string]testcase{
    26  		"linux": {
    27  			InputGOOS: "linux",
    28  			InputEnv: map[string]string{
    29  				"HOME": "/realhome",
    30  			},
    31  			ExpectedHomeDir:   "/realhome",
    32  			ExpectedCacheDir:  "/realhome/.cache",
    33  			ExpectedConfigDir: "/realhome/.config",
    34  		},
    35  		"linux-withhome": {
    36  			InputGOOS: "linux",
    37  			InputHOME: "/testhome",
    38  			InputEnv: map[string]string{
    39  				"HOME": "/realhome",
    40  			},
    41  			ExpectedHomeDir:   "/testhome",
    42  			ExpectedCacheDir:  "/testhome/.cache",
    43  			ExpectedConfigDir: "/testhome/.config",
    44  		},
    45  		"linux-xdg": {
    46  			InputGOOS: "linux",
    47  			InputEnv: map[string]string{
    48  				"HOME":            "/realhome",
    49  				"XDG_CACHE_HOME":  "/realhome/xdg-cache",
    50  				"XDG_CONFIG_HOME": "/realhome/xdg-config",
    51  			},
    52  			ExpectedHomeDir:   "/realhome",
    53  			ExpectedCacheDir:  "/realhome/xdg-cache",
    54  			ExpectedConfigDir: "/realhome/xdg-config",
    55  		},
    56  		"linux-xdg-withhome": {
    57  			InputGOOS: "linux",
    58  			InputHOME: "/testhome",
    59  			InputEnv: map[string]string{
    60  				"HOME":            "/realhome",
    61  				"XDG_CACHE_HOME":  "/realhome/xdg-config",
    62  				"XDG_CONFIG_HOME": "/realhome/xdg-config",
    63  			},
    64  			ExpectedHomeDir:   "/testhome",
    65  			ExpectedCacheDir:  "/testhome/.cache",
    66  			ExpectedConfigDir: "/testhome/.config",
    67  		},
    68  		"darwin": {
    69  			InputGOOS: "darwin",
    70  			InputEnv: map[string]string{
    71  				"HOME": "/realhome",
    72  			},
    73  			ExpectedHomeDir:   "/realhome",
    74  			ExpectedCacheDir:  "/realhome/Library/Caches",
    75  			ExpectedConfigDir: "/realhome/Library/Application Support",
    76  		},
    77  		"darwin-withhome": {
    78  			InputGOOS: "darwin",
    79  			InputHOME: "/testhome",
    80  			InputEnv: map[string]string{
    81  				"HOME": "/realhome",
    82  			},
    83  			ExpectedHomeDir:   "/testhome",
    84  			ExpectedCacheDir:  "/testhome/Library/Caches",
    85  			ExpectedConfigDir: "/testhome/Library/Application Support",
    86  		},
    87  	}
    88  	origEnv := os.Environ()
    89  	for tcName, tcData := range testcases {
    90  		tcData := tcData
    91  		t.Run(tcName, func(t *testing.T) {
    92  			if runtime.GOOS == "windows" {
    93  				splitAndRejoin := func(path *string) {
    94  					if path != nil && *path != "" {
    95  						ps := strings.Split(*path, "/")
    96  						if ps[0] == "" {
    97  							ps[0] = "\\"
    98  						}
    99  						*path = filepath.Join(ps...)
   100  					}
   101  				}
   102  				splitAndRejoinEnv := func(env map[string]string) {
   103  					for k, v := range env {
   104  						splitAndRejoin(&v)
   105  						env[k] = v
   106  					}
   107  				}
   108  				splitAndRejoin(&tcData.InputHOME)
   109  				splitAndRejoin(&tcData.ExpectedHomeDir)
   110  				splitAndRejoin(&tcData.ExpectedCacheDir)
   111  				splitAndRejoin(&tcData.ExpectedConfigDir)
   112  				splitAndRejoinEnv(tcData.InputEnv)
   113  			}
   114  			ctx := dlog.NewTestContext(t, true)
   115  			defer func() {
   116  				os.Clearenv()
   117  				for _, kv := range origEnv {
   118  					parts := strings.SplitN(kv, "=", 2)
   119  					if len(parts) != 2 {
   120  						continue
   121  					}
   122  					os.Setenv(parts[0], parts[1])
   123  				}
   124  			}()
   125  
   126  			// Given...
   127  			ctx = WithGOOS(ctx, tcData.InputGOOS)
   128  			if tcData.InputHOME != "" {
   129  				ctx = WithUserHomeDir(ctx, tcData.InputHOME)
   130  			}
   131  			os.Clearenv()
   132  			for k, v := range tcData.InputEnv {
   133  				os.Setenv(k, v)
   134  			}
   135  
   136  			// Then do...
   137  			actualHomePath := UserHomeDir(ctx)
   138  			actualCachePath := UserCacheDir(ctx)
   139  			actualConfigPath := UserConfigDir(ctx)
   140  
   141  			// And expect...
   142  
   143  			assert.Equal(t, tcData.ExpectedHomeDir, actualHomePath)
   144  			assert.Equal(t, tcData.ExpectedCacheDir, actualCachePath)
   145  			assert.Equal(t, tcData.ExpectedConfigDir, actualConfigPath)
   146  		})
   147  	}
   148  }