-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathutils_test.go
More file actions
145 lines (129 loc) · 3.65 KB
/
Copy pathutils_test.go
File metadata and controls
145 lines (129 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package cliutils
import (
"os"
"path/filepath"
"testing"
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
testUtils "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/stretchr/testify/assert"
)
func TestReadJFrogApplicationKeyFromConfigOrEnv(t *testing.T) {
configFilePath := filepath.Join(JfConfigDirName, JfConfigFileName)
// Test cases
tests := []struct {
name string
configContent string
envValue string
expectedResult string
}{
{
name: "Application key in config file",
configContent: "application:\n key: configKey",
envValue: "",
expectedResult: "configKey",
},
{
name: "Application key in environment variable",
configContent: "",
envValue: "envKey",
expectedResult: "envKey",
},
{
name: "Application key in both config file and environment variable",
configContent: "application:\n key: configKey",
envValue: "envKey",
expectedResult: "envKey",
},
{
name: "No application key in config file or environment variable",
configContent: "",
envValue: "",
expectedResult: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup temp dir for each test
testDirPath, err := filepath.Abs(filepath.Join("../", "tests", "applicationConfigTestDir"))
assert.NoError(t, err)
_, cleanUp := testUtils.CreateTestWorkspace(t, testDirPath)
// Write config content to file
if tt.configContent != "" {
err = os.WriteFile(configFilePath, []byte(tt.configContent), 0644)
assert.NoError(t, err)
}
// Set environment variable
if tt.envValue != "" {
assert.NoError(t, os.Setenv(coreutils.ApplicationKey, tt.envValue))
} else {
assert.NoError(t, os.Unsetenv(coreutils.ApplicationKey))
}
// Call the function
result := ReadJFrogApplicationKeyFromConfigOrEnv()
// Assert the result
assert.Equal(t, tt.expectedResult, result)
// delete temp folder
cleanUp()
})
}
}
func TestShouldOfferConfig_AgentSkipsPrompt(t *testing.T) {
tests := []struct {
name string
ciEnv string
agentEnv string // CLAUDE_CODE_CHILD_SESSION to simulate an agent
wantOffer bool
wantErr bool
}{
{
name: "agent env set — no prompt",
agentEnv: "1",
wantOffer: false,
},
{
name: "CI=true — no prompt",
ciEnv: "true",
wantOffer: false,
},
{
name: "CI=true and agent set — no prompt",
ciEnv: "true",
agentEnv: "1",
wantOffer: false,
},
{
name: "neither CI nor agent — would prompt (AskYesNo returns false on non-TTY)",
wantOffer: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Ensure no server config exists for the duration of this subtest.
_, cleanUp := testUtils.CreateTestWorkspace(t, t.TempDir())
defer cleanUp()
// Reset the memoized execution context so our env var changes take effect.
commands.ResetExecutionContextForTest()
defer commands.ResetExecutionContextForTest()
if tt.ciEnv != "" {
assert.NoError(t, os.Setenv(coreutils.CI, tt.ciEnv))
defer func() { _ = os.Unsetenv(coreutils.CI) }()
} else {
_ = os.Unsetenv(coreutils.CI)
}
if tt.agentEnv != "" {
assert.NoError(t, os.Setenv("CLAUDE_CODE_CHILD_SESSION", tt.agentEnv))
defer func() { _ = os.Unsetenv("CLAUDE_CODE_CHILD_SESSION") }()
} else {
_ = os.Unsetenv("CLAUDE_CODE_CHILD_SESSION")
}
offer, err := ShouldOfferConfig()
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.wantOffer, offer)
})
}
}