1package config
2
3import (
4 "testing"
5)
6
7func TestHasEnvSource(t *testing.T) {
8 tests := []struct {
9 name string
10 environ map[string]string
11 envKey string
12 procEnv map[string]string // simulated process env
13 want bool
14 }{
15 {
16 name: "direct key present",
17 environ: map[string]string{"RESTIC_REPOSITORY": "s3:bucket"},
18 envKey: "RESTIC_REPOSITORY",
19 want: true,
20 },
21 {
22 name: "command variant present",
23 environ: map[string]string{"RESTIC_REPOSITORY_COMMAND": "op read ..."},
24 envKey: "RESTIC_REPOSITORY",
25 want: true,
26 },
27 {
28 name: "neither direct nor command",
29 environ: map[string]string{"OTHER_VAR": "value"},
30 envKey: "RESTIC_REPOSITORY",
31 want: false,
32 },
33 {
34 name: "nil environ",
35 environ: nil,
36 envKey: "RESTIC_REPOSITORY",
37 want: false,
38 },
39 {
40 name: "empty string value counts as absent",
41 environ: map[string]string{"RESTIC_REPOSITORY": ""},
42 envKey: "RESTIC_REPOSITORY",
43 want: false,
44 },
45 {
46 name: "command variant for non-restic key",
47 environ: map[string]string{"AWS_ACCESS_KEY_ID_COMMAND": "op read ..."},
48 envKey: "AWS_ACCESS_KEY_ID",
49 want: true,
50 },
51 {
52 name: "process env fallback",
53 environ: nil,
54 envKey: "KELD_TEST_HAS_ENV_SOURCE",
55 procEnv: map[string]string{"KELD_TEST_HAS_ENV_SOURCE": "from-process"},
56 want: true,
57 },
58 {
59 name: "process env empty when not set",
60 environ: nil,
61 envKey: "KELD_TEST_HAS_ENV_SOURCE",
62 want: false,
63 },
64 }
65
66 for _, tt := range tests {
67 t.Run(tt.name, func(t *testing.T) {
68 // Tests using t.Setenv cannot run in parallel.
69 if len(tt.procEnv) == 0 {
70 t.Parallel()
71 }
72
73 for k, v := range tt.procEnv {
74 t.Setenv(k, v)
75 }
76
77 cfg := &ResolvedConfig{
78 Environ: tt.environ,
79 }
80
81 got := cfg.HasEnvSource(tt.envKey)
82 if got != tt.want {
83 t.Errorf("HasEnvSource(%q) = %v, want %v", tt.envKey, got, tt.want)
84 }
85 })
86 }
87}