github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/home_nix_test.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  //go:build !windows
     5  // +build !windows
     6  
     7  package libkb
     8  
     9  import (
    10  	"os"
    11  	"path"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestPosix(t *testing.T) {
    19  	hf := NewHomeFinder("tester", nil, nil, nil, "posix", func() RunMode { return ProductionRunMode },
    20  		makeLogGetter(t), nil)
    21  	d := hf.CacheDir()
    22  	if !strings.Contains(d, ".cache/tester") {
    23  		t.Errorf("Bad Cache dir: %s", d)
    24  	}
    25  	d = hf.DataDir()
    26  	if !strings.Contains(d, ".local/share/tester") {
    27  		t.Errorf("Bad Data dir: %s", d)
    28  	}
    29  	d = hf.ConfigDir()
    30  	if !strings.Contains(d, ".config/tester") {
    31  		t.Errorf("Bad Config dir: %s", d)
    32  	}
    33  }
    34  
    35  func TestDarwinHomeFinder(t *testing.T) {
    36  	for _, osname := range []string{"darwin", "ios"} {
    37  		hf := NewHomeFinder("keybase", nil, nil, nil, osname, func() RunMode { return ProductionRunMode }, makeLogGetter(t), nil)
    38  		d := hf.ConfigDir()
    39  		if !strings.HasSuffix(d, "Library/Application Support/Keybase") {
    40  			t.Errorf("Bad config dir: %s", d)
    41  		}
    42  		d = hf.CacheDir()
    43  		if !strings.HasSuffix(d, "Library/Caches/Keybase") {
    44  			t.Errorf("Bad cache dir: %s", d)
    45  		}
    46  		hfInt := NewHomeFinder("keybase", func() string { return "home" }, nil, func() string { return "mobilehome" },
    47  			osname, func() RunMode { return ProductionRunMode }, makeLogGetter(t), nil)
    48  		hfDarwin := hfInt.(Darwin)
    49  		hfDarwin.forceIOS = true
    50  		hf = hfDarwin
    51  		d = hf.ConfigDir()
    52  		require.True(t, strings.HasSuffix(d, "Library/Application Support/Keybase"))
    53  		require.True(t, strings.HasPrefix(d, "mobilehome"))
    54  		d = hf.DataDir()
    55  		require.True(t, strings.HasSuffix(d, "Library/Application Support/Keybase"))
    56  		require.False(t, strings.HasPrefix(d, "mobilehome"))
    57  		require.True(t, strings.HasPrefix(d, "home"))
    58  	}
    59  }
    60  
    61  func TestDarwinHomeFinderInDev(t *testing.T) {
    62  	devHomeFinder := NewHomeFinder("keybase", nil, nil, nil, "darwin", func() RunMode { return DevelRunMode }, makeLogGetter(t), nil)
    63  	configDir := devHomeFinder.ConfigDir()
    64  	if !strings.HasSuffix(configDir, "Library/Application Support/KeybaseDevel") {
    65  		t.Errorf("Bad config dir: %s", configDir)
    66  	}
    67  	cacheDir := devHomeFinder.CacheDir()
    68  	if !strings.HasSuffix(cacheDir, "Library/Caches/KeybaseDevel") {
    69  		t.Errorf("Bad cache dir: %s", cacheDir)
    70  	}
    71  }
    72  
    73  func TestPosixRuntimeDir(t *testing.T) {
    74  	var cmdHome string
    75  	env := make(map[string]string)
    76  	ge := func(s string) string { return env[s] }
    77  	hf := NewHomeFinder("tester", func() string { return cmdHome }, nil, nil, "posix", func() RunMode { return ProductionRunMode }, makeLogGetter(t), ge)
    78  
    79  	origHomeEnv := os.Getenv("HOME")
    80  
    81  	// Custom env, custom cmd, XDG set
    82  	cmdHome = "/footown"
    83  	env["HOME"] = "/yoyo"
    84  	env["XDG_RUNTIME_DIR"] = "/barland"
    85  	require.Equal(t, "/footown/.config/tester", hf.RuntimeDir(), "expect custom cmd to win")
    86  
    87  	// Custom env, no cmd, XDG set
    88  	cmdHome = ""
    89  	env["HOME"] = "/yoyo"
    90  	env["XDG_RUNTIME_DIR"] = "/barland"
    91  	require.Equal(t, "/yoyo/.config/tester", hf.RuntimeDir(), "expect custom env to win")
    92  
    93  	// Standard env, no cmd, XDG set
    94  	cmdHome = ""
    95  	env["HOME"] = origHomeEnv
    96  	env["XDG_RUNTIME_DIR"] = "/barland"
    97  	require.Equal(t, "/barland/tester", hf.RuntimeDir(), "expect xdg to win")
    98  
    99  	// Standard env, no cmd, XDG unset
   100  	cmdHome = ""
   101  	env["HOME"] = origHomeEnv
   102  	delete(env, "XDG_RUNTIME_DIR")
   103  	require.Equal(t, path.Join(origHomeEnv, ".config", "tester"), hf.RuntimeDir(), "expect home to win")
   104  }