github.com/takumakei/go-urfave-cli/clix@v0.0.0-20210528051825-f43cf228bf5d/filepath_test.go (about) 1 package clix_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/takumakei/go-urfave-cli/clix" 10 "github.com/urfave/cli/v2" 11 ) 12 13 func ExampleFilePath() { 14 // This flag can be set in 3 ways. 15 // 16 // 1. a command line argument `--password` 17 // 2. an environment variable `EXAMPLE_PASSWORD` 18 // 3. one of the first file that exists 19 // 1. pathname by the environment variable `EXAMPLE_PASSWORD_FILE` 20 // 2. or pathname of `$HOME/.config/example/defaults/password` 21 // 3. or pathname of `/etc/example/defaults/password` 22 flag := &cli.StringFlag{ 23 Name: "password", 24 EnvVars: []string{"EXAMPLE_PASSWORD"}, 25 FilePath: clix.FilePath( 26 os.Getenv("EXAMPLE_PASSWORD_FILE"), 27 os.ExpandEnv("$HOME/.config/example/defaults/password"), 28 "/etc/example/defaults/password", 29 ), 30 } 31 _ = flag 32 } 33 34 func TestFilePath(t *testing.T) { 35 dir, err := ioutil.TempDir("", "example") 36 if err != nil { 37 t.Fatal(err) 38 } 39 t.Cleanup(func() { os.RemoveAll(dir) }) 40 41 f1st := filepath.Join(dir, "1st") 42 f2nd := filepath.Join(dir, "2nd") 43 f3rd := filepath.Join(dir, "3rd") 44 45 t.Run("Default", func(t *testing.T) { 46 // no file, no env, no arg 47 want := "default" 48 testFilePath(t, want, nil, f2nd, f3rd) 49 }) 50 51 t.Cleanup(func() { os.Unsetenv("EXAMPLE_PASSWORD_FILE") }) 52 t.Run("ByFilePath", func(t *testing.T) { 53 t.Run("3rd", func(t *testing.T) { 54 // only 3rd file exists 55 want := "3rd" 56 if err := ioutil.WriteFile(f3rd, []byte(want), 0644); err != nil { 57 t.Fatal(err) 58 } 59 testFilePath(t, want, nil, f2nd, f3rd) 60 }) 61 t.Run("2nd", func(t *testing.T) { 62 // 2nd and 3rd files exist 63 want := "2nd" 64 if err := ioutil.WriteFile(f2nd, []byte(want), 0644); err != nil { 65 t.Fatal(err) 66 } 67 testFilePath(t, want, nil, f2nd, f3rd) 68 }) 69 t.Run("1st", func(t *testing.T) { 70 // 1st, 2nd and 3rd files exist 71 want := "1st" 72 if err := ioutil.WriteFile(f1st, []byte(want), 0644); err != nil { 73 t.Fatal(err) 74 } 75 os.Setenv("EXAMPLE_PASSWORD_FILE", f1st) 76 testFilePath(t, want, nil, f2nd, f3rd) 77 }) 78 }) 79 80 t.Cleanup(func() { os.Unsetenv("EXAMPLE_PASSWORD") }) 81 t.Run("ByEnv", func(t *testing.T) { 82 want := "environment variable" 83 os.Setenv("EXAMPLE_PASSWORD", want) 84 testFilePath(t, want, nil, f2nd, f3rd) 85 }) 86 t.Run("ByArg", func(t *testing.T) { 87 want := "command line" 88 testFilePath(t, want, []string{"--password", want}, f2nd, f3rd) 89 }) 90 } 91 92 func testFilePath(t *testing.T, want string, args []string, f2nd, f3rd string) { 93 t.Helper() 94 95 got := "" 96 app := cli.NewApp() 97 app.Flags = []cli.Flag{ 98 &cli.StringFlag{ 99 Name: "password", 100 EnvVars: []string{"EXAMPLE_PASSWORD"}, 101 FilePath: clix.FilePath( 102 os.Getenv("EXAMPLE_PASSWORD_FILE"), 103 f2nd, 104 f3rd, 105 ), 106 Value: "default", 107 }, 108 } 109 app.Action = func(c *cli.Context) error { 110 got = c.String("password") 111 return nil 112 } 113 _ = app.Run(append([]string{"prog"}, args...)) 114 if got != want { 115 t.Errorf("got %q, want %q", got, want) 116 } 117 }