-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathConfigHandlerTests.swift
More file actions
153 lines (127 loc) · 4.82 KB
/
ConfigHandlerTests.swift
File metadata and controls
153 lines (127 loc) · 4.82 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
146
147
148
149
150
151
152
153
import Foundation
import Logging
import Testing
@testable import xcodeinstall
@Suite("ConfigHandler Tests", .serialized)
struct ConfigHandlerTests {
let log: Logger = Logger(label: "ConfigHandlerTests")
var fileManager: FileManager
init() {
self.fileManager = FileManager.default
}
@Test("Save and load config")
func testSaveAndLoad() throws {
try withTemporaryDirectory { tempDir in
// Given
let configHandler = ConfigHandler(log: log, baseDirectory: tempDir)
let config = PersistentConfig(
secretManagerRegion: "us-west-2",
profileName: "myprofile"
)
// When
try configHandler.saveConfig(config)
let loadedConfig = configHandler.loadConfig()
// Then
#expect(loadedConfig != nil)
#expect(loadedConfig?.secretManagerRegion == "us-west-2")
#expect(loadedConfig?.profileName == "myprofile")
}
}
@Test("Load non-existent file returns nil")
func testLoadNonExistent() throws {
try withTemporaryDirectory { tempDir in
// Given
let configHandler = ConfigHandler(log: log, baseDirectory: tempDir)
// When
let loadedConfig = configHandler.loadConfig()
// Then
#expect(loadedConfig == nil)
}
}
@Test("Load corrupted JSON returns nil")
func testLoadCorrupted() throws {
try withTemporaryDirectory { tempDir in
// Given
let configHandler = ConfigHandler(log: log, baseDirectory: tempDir)
// Create directory and write invalid JSON
try fileManager.createDirectory(at: tempDir, withIntermediateDirectories: true)
let invalidJSON = "invalid json {{{".data(using: .utf8)!
try invalidJSON.write(to: configHandler.configPath())
// When
let loadedConfig = configHandler.loadConfig()
// Then
#expect(loadedConfig == nil)
}
}
@Test("Save partial config - only region")
func testPartialConfigRegionOnly() throws {
try withTemporaryDirectory { tempDir in
// Given
let configHandler = ConfigHandler(log: log, baseDirectory: tempDir)
let config = PersistentConfig(
secretManagerRegion: "us-east-1",
profileName: nil
)
// When
try configHandler.saveConfig(config)
let loadedConfig = configHandler.loadConfig()
// Then
#expect(loadedConfig != nil)
#expect(loadedConfig?.secretManagerRegion == "us-east-1")
#expect(loadedConfig?.profileName == nil)
}
}
@Test("Save partial config - only profile")
func testPartialConfigProfileOnly() throws {
try withTemporaryDirectory { tempDir in
// Given
let configHandler = ConfigHandler(log: log, baseDirectory: tempDir)
let config = PersistentConfig(
secretManagerRegion: nil,
profileName: "testprofile"
)
// When
try configHandler.saveConfig(config)
let loadedConfig = configHandler.loadConfig()
// Then
#expect(loadedConfig != nil)
#expect(loadedConfig?.secretManagerRegion == nil)
#expect(loadedConfig?.profileName == "testprofile")
}
}
@Test("Overwrite existing config")
func testOverwrite() throws {
try withTemporaryDirectory { tempDir in
// Given
let configHandler = ConfigHandler(log: log, baseDirectory: tempDir)
let initialConfig = PersistentConfig(
secretManagerRegion: "us-west-1",
profileName: "profile1"
)
let updatedConfig = PersistentConfig(
secretManagerRegion: "eu-west-1",
profileName: "profile2"
)
// When
try configHandler.saveConfig(initialConfig)
try configHandler.saveConfig(updatedConfig)
let loadedConfig = configHandler.loadConfig()
// Then
#expect(loadedConfig != nil)
#expect(loadedConfig?.secretManagerRegion == "eu-west-1")
#expect(loadedConfig?.profileName == "profile2")
}
}
@Test("Config path returns correct location")
func testConfigPath() throws {
try withTemporaryDirectory { tempDir in
// Given
let configHandler = ConfigHandler(log: log, baseDirectory: tempDir)
// When
let path = configHandler.configPath()
// Then
#expect(path.lastPathComponent == "config.json")
#expect(path.deletingLastPathComponent().path == tempDir.path)
}
}
}