Skip to content

Commit b419603

Browse files
committed
refactor: Improve test structure with shared data and individual cases
- Move test data creation to Context BeforeAll level - Restore individual test cases (16 total) for focused testing - Eliminate data duplication while keeping inline visibility - Best of both worlds: shared setup + granular test cases Structure: - Context BeforeAll: Creates shared test files with inline data - Individual It blocks: Reference shared files for specific scenarios - Clear test names and focused assertions per test case
1 parent 2f572cc commit b419603

File tree

1 file changed

+136
-123
lines changed

1 file changed

+136
-123
lines changed

updater/tests/update-dependency-cmake.Tests.ps1

Lines changed: 136 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ BeforeAll {
55

66
Describe 'CMake Helper Functions' {
77
Context 'Parse-CMakeFetchContent' {
8-
BeforeEach {
8+
BeforeAll {
99
$script:tempDir = "$TestDrive/cmake-tests"
1010
New-Item $tempDir -ItemType Directory -Force | Out-Null
11-
}
1211

13-
It 'parses FetchContent_Declare with various scenarios' {
14-
# Test 1: Basic parsing with explicit dependency name
15-
$testFile1 = "$tempDir/basic.cmake"
12+
# Create test files with inline data
13+
$script:basicFile = "$tempDir/basic.cmake"
1614
@'
1715
include(FetchContent)
1816
@@ -24,21 +22,9 @@ FetchContent_Declare(
2422
)
2523
2624
FetchContent_MakeAvailable(sentry-native)
27-
'@ | Out-File $testFile1
25+
'@ | Out-File $basicFile
2826

29-
$result = Parse-CMakeFetchContent $testFile1 'sentry-native'
30-
$result.GitRepository | Should -Be 'https://github.com/getsentry/sentry-native'
31-
$result.GitTag | Should -Be 'v0.9.1'
32-
$result.DepName | Should -Be 'sentry-native'
33-
34-
# Test 2: Auto-detection with same file
35-
$result = Parse-CMakeFetchContent $testFile1 $null
36-
$result.GitRepository | Should -Be 'https://github.com/getsentry/sentry-native'
37-
$result.GitTag | Should -Be 'v0.9.1'
38-
$result.DepName | Should -Be 'sentry-native'
39-
40-
# Test 3: Hash values
41-
$testFile2 = "$tempDir/hash.cmake"
27+
$script:hashFile = "$tempDir/hash.cmake"
4228
@'
4329
include(FetchContent)
4430
@@ -51,15 +37,9 @@ FetchContent_Declare(
5137
)
5238
5339
FetchContent_MakeAvailable(sentry-native)
54-
'@ | Out-File $testFile2
55-
56-
$result = Parse-CMakeFetchContent $testFile2 'sentry-native'
57-
$result.GitRepository | Should -Be 'https://github.com/getsentry/sentry-native'
58-
$result.GitTag | Should -Be 'a64d5bd8ee130f2cda196b6fa7d9b65bfa6d32e2'
59-
$result.DepName | Should -Be 'sentry-native'
40+
'@ | Out-File $hashFile
6041

61-
# Test 4: Complex multi-line formatting
62-
$testFile3 = "$tempDir/complex.cmake"
42+
$script:complexFile = "$tempDir/complex.cmake"
6343
@'
6444
include(FetchContent)
6545
@@ -76,16 +56,9 @@ FetchContent_Declare(
7656
)
7757
7858
FetchContent_MakeAvailable(sentry-native)
79-
'@ | Out-File $testFile3
59+
'@ | Out-File $complexFile
8060

81-
$result = Parse-CMakeFetchContent $testFile3 'sentry-native'
82-
$result.GitRepository | Should -Be 'https://github.com/getsentry/sentry-native'
83-
$result.GitTag | Should -Be 'v0.9.1'
84-
$result.DepName | Should -Be 'sentry-native'
85-
}
86-
87-
It 'handles multiple dependencies correctly' {
88-
$testFile = "$tempDir/multiple.cmake"
61+
$script:multipleFile = "$tempDir/multiple.cmake"
8962
@'
9063
include(FetchContent)
9164
@@ -102,76 +75,104 @@ FetchContent_Declare(
10275
)
10376
10477
FetchContent_MakeAvailable(sentry-native googletest)
105-
'@ | Out-File $testFile
106-
107-
# Should throw when no explicit name given
108-
{ Parse-CMakeFetchContent $testFile $null } | Should -Throw '*Multiple FetchContent declarations found*'
109-
110-
# Should work with explicit dependency name
111-
$result = Parse-CMakeFetchContent $testFile 'googletest'
112-
$result.GitRepository | Should -Be 'https://github.com/google/googletest'
113-
$result.GitTag | Should -Be 'v1.14.0'
114-
$result.DepName | Should -Be 'googletest'
115-
}
78+
'@ | Out-File $multipleFile
11679

117-
It 'throws appropriate errors for invalid scenarios' {
118-
# Test 1: Missing dependency
119-
$testFile1 = "$tempDir/valid.cmake"
80+
$script:missingRepoFile = "$tempDir/missing-repo.cmake"
12081
@'
12182
include(FetchContent)
12283
12384
FetchContent_Declare(
12485
sentry-native
125-
GIT_REPOSITORY https://github.com/getsentry/sentry-native
12686
GIT_TAG v0.9.1
12787
)
128-
'@ | Out-File $testFile1
129-
130-
{ Parse-CMakeFetchContent $testFile1 'nonexistent' } | Should -Throw "*FetchContent_Declare for 'nonexistent' not found*"
88+
'@ | Out-File $missingRepoFile
13189

132-
# Test 2: Missing GIT_REPOSITORY
133-
$testFile2 = "$tempDir/missing-repo.cmake"
90+
$script:missingTagFile = "$tempDir/missing-tag.cmake"
13491
@'
13592
include(FetchContent)
13693
13794
FetchContent_Declare(
13895
sentry-native
139-
GIT_TAG v0.9.1
96+
GIT_REPOSITORY https://github.com/getsentry/sentry-native
14097
)
141-
'@ | Out-File $testFile2
98+
'@ | Out-File $missingTagFile
99+
}
142100

143-
{ Parse-CMakeFetchContent $testFile2 'sentry-native' } | Should -Throw '*Could not parse GIT_REPOSITORY or GIT_TAG*'
101+
It 'parses basic FetchContent_Declare with explicit dependency name' {
102+
$result = Parse-CMakeFetchContent $basicFile 'sentry-native'
144103

145-
# Test 3: Missing GIT_TAG
146-
$testFile3 = "$tempDir/missing-tag.cmake"
147-
@'
148-
include(FetchContent)
104+
$result.GitRepository | Should -Be 'https://github.com/getsentry/sentry-native'
105+
$result.GitTag | Should -Be 'v0.9.1'
106+
$result.DepName | Should -Be 'sentry-native'
107+
}
149108

150-
FetchContent_Declare(
151-
sentry-native
152-
GIT_REPOSITORY https://github.com/getsentry/sentry-native
153-
)
154-
'@ | Out-File $testFile3
109+
It 'auto-detects single FetchContent_Declare' {
110+
$result = Parse-CMakeFetchContent $basicFile $null
155111

156-
{ Parse-CMakeFetchContent $testFile3 'sentry-native' } | Should -Throw '*Could not parse GIT_REPOSITORY or GIT_TAG*'
112+
$result.GitRepository | Should -Be 'https://github.com/getsentry/sentry-native'
113+
$result.GitTag | Should -Be 'v0.9.1'
114+
$result.DepName | Should -Be 'sentry-native'
115+
}
116+
117+
It 'handles hash values correctly' {
118+
$result = Parse-CMakeFetchContent $hashFile 'sentry-native'
119+
120+
$result.GitRepository | Should -Be 'https://github.com/getsentry/sentry-native'
121+
$result.GitTag | Should -Be 'a64d5bd8ee130f2cda196b6fa7d9b65bfa6d32e2'
122+
$result.DepName | Should -Be 'sentry-native'
123+
}
124+
125+
It 'handles complex multi-line formatting' {
126+
$result = Parse-CMakeFetchContent $complexFile 'sentry-native'
127+
128+
$result.GitRepository | Should -Be 'https://github.com/getsentry/sentry-native'
129+
$result.GitTag | Should -Be 'v0.9.1'
130+
$result.DepName | Should -Be 'sentry-native'
131+
}
132+
133+
It 'throws on multiple dependencies without explicit name' {
134+
{ Parse-CMakeFetchContent $multipleFile $null } | Should -Throw '*Multiple FetchContent declarations found*'
135+
}
136+
137+
It 'handles specific dependency from multiple dependencies' {
138+
$result = Parse-CMakeFetchContent $multipleFile 'googletest'
139+
140+
$result.GitRepository | Should -Be 'https://github.com/google/googletest'
141+
$result.GitTag | Should -Be 'v1.14.0'
142+
$result.DepName | Should -Be 'googletest'
143+
}
144+
145+
It 'throws on missing dependency' {
146+
{ Parse-CMakeFetchContent $basicFile 'nonexistent' } | Should -Throw "*FetchContent_Declare for 'nonexistent' not found*"
147+
}
148+
149+
It 'throws on missing GIT_REPOSITORY' {
150+
{ Parse-CMakeFetchContent $missingRepoFile 'sentry-native' } | Should -Throw '*Could not parse GIT_REPOSITORY or GIT_TAG*'
151+
}
152+
153+
It 'throws on missing GIT_TAG' {
154+
{ Parse-CMakeFetchContent $missingTagFile 'sentry-native' } | Should -Throw '*Could not parse GIT_REPOSITORY or GIT_TAG*'
157155
}
158156
}
159157

160158
Context 'Find-TagForHash' {
161-
It 'handles hash resolution scenarios' {
162-
# Test 1: Hash without matching tag
159+
It 'returns null for hash without matching tag' {
160+
# Use a fake hash that won't match any real tag
163161
$fakeHash = 'abcdef1234567890abcdef1234567890abcdef12'
164162
$repo = 'https://github.com/getsentry/sentry-native'
165163

166164
$result = Find-TagForHash $repo $fakeHash
165+
167166
$result | Should -BeNullOrEmpty
167+
}
168168

169-
# Test 2: Network failures
169+
It 'handles network failures gracefully' {
170170
$invalidRepo = 'https://github.com/nonexistent/repo'
171171
$hash = 'abcdef1234567890abcdef1234567890abcdef12'
172172

173173
# Should not throw, but return null and show warning
174174
$result = Find-TagForHash $invalidRepo $hash
175+
175176
$result | Should -BeNullOrEmpty
176177
}
177178

@@ -180,15 +181,12 @@ FetchContent_Declare(
180181
}
181182

182183
Context 'Update-CMakeFile' {
183-
BeforeEach {
184-
$script:tempDir = "$TestDrive/cmake-tests"
184+
BeforeAll {
185+
$script:tempDir = "$TestDrive/cmake-update-tests"
185186
New-Item $tempDir -ItemType Directory -Force | Out-Null
186-
}
187187

188-
It 'updates CMake files preserving format and structure' {
189-
# Test 1: Tag to tag update
190-
$testFile1 = "$tempDir/tag-update.cmake"
191-
@'
188+
# Template for basic CMake file
189+
$script:basicTemplate = @'
192190
include(FetchContent)
193191
194192
FetchContent_Declare(
@@ -199,22 +197,10 @@ FetchContent_Declare(
199197
)
200198
201199
FetchContent_MakeAvailable(sentry-native)
202-
'@ | Out-File $testFile1
203-
204-
Update-CMakeFile $testFile1 'sentry-native' 'v0.9.2'
200+
'@
205201

206-
$content = Get-Content $testFile1 -Raw
207-
$content | Should -Match 'GIT_TAG v0.9.2'
208-
$content | Should -Not -Match 'v0.9.1'
209-
# Verify structure preservation
210-
$content | Should -Match 'include\(FetchContent\)'
211-
$content | Should -Match 'FetchContent_MakeAvailable'
212-
$content | Should -Match 'GIT_REPOSITORY https://github.com/getsentry/sentry-native'
213-
$content | Should -Match 'GIT_SHALLOW FALSE'
214-
215-
# Test 2: Hash to newer hash update
216-
$testFile2 = "$tempDir/hash-update.cmake"
217-
@'
202+
# Template for hash-based CMake file
203+
$script:hashTemplate = @'
218204
include(FetchContent)
219205
220206
FetchContent_Declare(
@@ -226,21 +212,10 @@ FetchContent_Declare(
226212
)
227213
228214
FetchContent_MakeAvailable(sentry-native)
229-
'@ | Out-File $testFile2
230-
231-
# Update to a newer tag that will be converted to hash (0.11.0 is known to exist)
232-
Update-CMakeFile $testFile2 'sentry-native' '0.11.0'
233-
234-
$content = Get-Content $testFile2 -Raw
235-
# Should have new hash with tag comment
236-
$content | Should -Match 'GIT_TAG [a-f0-9]{40} # 0.11.0'
237-
# Should not have old hash or old comment
238-
$content | Should -Not -Match 'a64d5bd8ee130f2cda196b6fa7d9b65bfa6d32e2'
239-
$content | Should -Not -Match '# 0.9.1'
215+
'@
240216

241-
# Test 3: Complex formatting
242-
$testFile3 = "$tempDir/complex-format.cmake"
243-
@'
217+
# Template for complex formatting
218+
$script:complexTemplate = @'
244219
include(FetchContent)
245220
246221
FetchContent_Declare(
@@ -256,29 +231,67 @@ FetchContent_Declare(
256231
)
257232
258233
FetchContent_MakeAvailable(sentry-native)
259-
'@ | Out-File $testFile3
234+
'@
235+
}
260236

261-
Update-CMakeFile $testFile3 'sentry-native' 'v0.9.2'
237+
BeforeEach {
238+
# Create fresh test files for each test
239+
$script:basicTestFile = "$tempDir/basic-test.cmake"
240+
$script:hashTestFile = "$tempDir/hash-test.cmake"
241+
$script:complexTestFile = "$tempDir/complex-test.cmake"
242+
}
262243

263-
$content = Get-Content $testFile3 -Raw
264-
$content | Should -Match 'GIT_TAG\s+v0.9.2'
244+
It 'updates tag to tag preserving format' {
245+
$basicTemplate | Out-File $basicTestFile
246+
247+
Update-CMakeFile $basicTestFile 'sentry-native' 'v0.9.2'
248+
249+
$content = Get-Content $basicTestFile -Raw
250+
$content | Should -Match 'GIT_TAG v0.9.2'
265251
$content | Should -Not -Match 'v0.9.1'
266252
}
267253

268-
It 'handles error scenarios appropriately' {
269-
$testFile = "$tempDir/error-test.cmake"
270-
@'
271-
include(FetchContent)
254+
It 'updates hash to newer hash preserving format' {
255+
$hashTemplate | Out-File $hashTestFile
272256

273-
FetchContent_Declare(
274-
sentry-native
275-
GIT_REPOSITORY https://github.com/getsentry/sentry-native
276-
GIT_TAG v0.9.1
277-
)
278-
'@ | Out-File $testFile
257+
# Update to a newer tag that will be converted to hash (0.11.0 is known to exist)
258+
Update-CMakeFile $hashTestFile 'sentry-native' '0.11.0'
259+
260+
$content = Get-Content $hashTestFile -Raw
261+
# Should have new hash with tag comment
262+
$content | Should -Match 'GIT_TAG [a-f0-9]{40} # 0.11.0'
263+
# Should not have old hash or old comment
264+
$content | Should -Not -Match 'a64d5bd8ee130f2cda196b6fa7d9b65bfa6d32e2'
265+
$content | Should -Not -Match '# 0.9.1'
266+
}
267+
268+
It 'preserves file structure and other content' {
269+
$basicTemplate | Out-File $basicTestFile
270+
271+
Update-CMakeFile $basicTestFile 'sentry-native' 'v0.9.2'
272+
273+
$content = Get-Content $basicTestFile -Raw
274+
$content | Should -Match 'include\(FetchContent\)'
275+
$content | Should -Match 'FetchContent_MakeAvailable'
276+
$content | Should -Match 'GIT_REPOSITORY https://github.com/getsentry/sentry-native'
277+
$content | Should -Match 'GIT_SHALLOW FALSE'
278+
}
279+
280+
It 'handles complex formatting correctly' {
281+
$complexTemplate | Out-File $complexTestFile
282+
283+
Update-CMakeFile $complexTestFile 'sentry-native' 'v0.9.2'
284+
285+
$content = Get-Content $complexTestFile -Raw
286+
$content | Should -Match 'GIT_TAG\s+v0.9.2'
287+
$content | Should -Not -Match 'v0.9.1'
288+
}
289+
290+
It 'throws on failed regex match' {
291+
$basicTemplate | Out-File $basicTestFile
279292

280293
# Try to update a dependency that doesn't exist
281-
{ Update-CMakeFile $testFile 'nonexistent-dep' 'v1.0.0' } | Should -Throw "*FetchContent_Declare for 'nonexistent-dep' not found*"
294+
{ Update-CMakeFile $basicTestFile 'nonexistent-dep' 'v1.0.0' } | Should -Throw "*FetchContent_Declare for 'nonexistent-dep' not found*"
282295
}
283296

284297
# Note: Hash update tests require network access for git ls-remote

0 commit comments

Comments
 (0)