Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.

Commit 03c485b

Browse files
authored
Merge pull request #319 from johlju/add-localization-tests
Add localization tests
2 parents adcd839 + ee9f1be commit 03c485b

3 files changed

Lines changed: 379 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@
3636
use AllowClobber
3737
([issue #310](https://github.com/PowerShell/DscResource.Tests/issues/310)).
3838
- Fix MetaFixers Functions Pipeline Processing.
39+
- Added common opt-in tests for localization ([issue #145](https://github.com/PowerShell/DscResource.Tests/issues/145)).
40+
- Should have an en-US localization folder.
41+
- The en-US localization folder should have the correct casing.
42+
- A resource file with the correct name should exist in the localization
43+
folder.
44+
- The resource or module should use all the localization string keys
45+
from the localization resource file.
46+
- The localization resource file should not be missing any localization
47+
string key that is used in the resource or module.
48+
- If there are other localization folders (other than en-US)
49+
- They should contain a resource file with the correct name.
50+
- The folders should use the correct casing.
51+
- All en-US resource file localized string keys must also exist in
52+
the resource file.
53+
- There should be no additional localized string keys in the resource
54+
file that do not exist in the en-US resource file.
3955

4056
## 0.3.0.0
4157

Meta.Tests.ps1

Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,3 +1243,341 @@ Describe 'Common Tests - Validate Markdown Links' -Tag 'Markdown' {
12431243
}
12441244
}
12451245
}
1246+
1247+
Describe 'Common Tests - Validate Localization' {
1248+
$optIn = Get-PesterDescribeOptInStatus -OptIns $optIns
1249+
1250+
# Due to verbose output, only run these test if opt-in.
1251+
if ($optIn)
1252+
{
1253+
$allFolders = Get-ChildItem -Path (Join-Path -Path $moduleRootFilePath -ChildPath 'DscResources') -Directory
1254+
$allFolders += Get-ChildItem -Path (Join-Path -Path $moduleRootFilePath -ChildPath 'Modules') -Directory
1255+
$allFolders = $allFolders | Sort-Object -Property Name
1256+
1257+
Context 'When a resource or module should have localization files' {
1258+
BeforeAll {
1259+
$foldersToTest = @()
1260+
1261+
foreach ($folder in $allFolders)
1262+
{
1263+
$foldersToTest += @{
1264+
Folder = $folder.Name
1265+
Path = $folder.FullName
1266+
}
1267+
}
1268+
}
1269+
1270+
It 'Should have en-US localization folder for the resource/module <Folder>' -TestCases $foldersToTest {
1271+
param
1272+
(
1273+
[Parameter()]
1274+
[System.String]
1275+
$Folder,
1276+
1277+
[Parameter()]
1278+
[System.String]
1279+
$Path
1280+
)
1281+
1282+
$localizationFolderPath = Join-Path -Path $Path -ChildPath 'en-US'
1283+
1284+
Test-Path -Path $localizationFolderPath | Should -BeTrue -Because 'the en-US folder must exist'
1285+
}
1286+
1287+
It 'Should have en-US localization folder with the correct casing for the resource/module <Folder>' -TestCases $foldersToTest {
1288+
param
1289+
(
1290+
[Parameter()]
1291+
[System.String]
1292+
$Folder,
1293+
1294+
[Parameter()]
1295+
[System.String]
1296+
$Path
1297+
)
1298+
1299+
<#
1300+
This will return both 'en-us' and 'en-US' folders so we can
1301+
evaluate casing.
1302+
#>
1303+
$localizationFolderOnDisk = Get-ChildItem -Path $Path -Directory -Filter 'en-US'
1304+
$localizationFolderOnDisk.Name | Should -MatchExactly 'en-US' -Because 'the en-US folder must have the correct casing'
1305+
}
1306+
1307+
It 'Should have en-US localization string resource file for the resource/module <Folder>' -TestCases $foldersToTest {
1308+
param
1309+
(
1310+
[Parameter()]
1311+
[System.String]
1312+
$Folder,
1313+
1314+
[Parameter()]
1315+
[System.String]
1316+
$Path
1317+
)
1318+
1319+
$localizationResourceFilePath = Join-Path -Path (Join-Path -Path $Path -ChildPath 'en-US') -ChildPath "$Folder.strings.psd1"
1320+
1321+
Test-Path -Path $localizationResourceFilePath | Should -BeTrue -Because 'there must exist a string resource file in the localization folder en-US'
1322+
}
1323+
1324+
foreach ($testCase in $foldersToTest)
1325+
{
1326+
$skipTest_LocalizedKeys = $false
1327+
$skipTest_UsedLocalizedKeys = $false
1328+
1329+
$testCases_LocalizedKeys = @()
1330+
$testCases_UsedLocalizedKeys = @()
1331+
1332+
$sourceLocalizationFolderPath = Join-Path -Path $testCase.Path -ChildPath 'en-US'
1333+
$localizationResourceFile = '{0}.strings.psd1' -f $testCase.Folder
1334+
1335+
# Skip files that do not exist yet (they were caught in a previous test above)
1336+
if (-not (Test-Path -Path (Join-Path -Path $sourceLocalizationFolderPath -ChildPath $localizationResourceFile)))
1337+
{
1338+
Write-Warning -Message ('Missing the localized string resource file ''{0}''' -f $testCase.Path)
1339+
1340+
continue
1341+
}
1342+
1343+
Import-LocalizedData `
1344+
-BindingVariable 'englishLocalizedStrings' `
1345+
-FileName $localizationResourceFile `
1346+
-BaseDirectory $sourceLocalizationFolderPath
1347+
1348+
foreach ($localizedKey in $englishLocalizedStrings.Keys)
1349+
{
1350+
$testCases_LocalizedKeys += @{
1351+
LocalizedKey = $localizedKey
1352+
}
1353+
}
1354+
1355+
$modulePath = Join-Path -Path $testCase.Path -ChildPath "$($testCase.Folder).psm1"
1356+
1357+
$parseErrors = $null
1358+
$definitionAst = [System.Management.Automation.Language.Parser]::ParseFile($modulePath, [ref] $null, [ref] $parseErrors)
1359+
1360+
if ($parseErrors)
1361+
{
1362+
throw $parseErrors
1363+
}
1364+
1365+
$astFilter = {
1366+
$args[0] -is [System.Management.Automation.Language.StringConstantExpressionAst] `
1367+
-and $args[0].Parent -is [System.Management.Automation.Language.MemberExpressionAst] `
1368+
-and $args[0].Parent.Expression -is [System.Management.Automation.Language.VariableExpressionAst] `
1369+
-and $args[0].Parent.Expression.VariablePath.UserPath -eq 'script:localizedData'
1370+
}
1371+
1372+
$localizationStringConstantsAst = $definitionAst.FindAll($astFilter, $true)
1373+
1374+
if ($localizationStringConstantsAst)
1375+
{
1376+
$usedLocalizationKeys = $localizationStringConstantsAst.Value | Sort-Object -Unique
1377+
1378+
foreach ($localizedKey in $usedLocalizationKeys)
1379+
{
1380+
$testCases_UsedLocalizedKeys += @{
1381+
LocalizedKey = $localizedKey
1382+
}
1383+
}
1384+
}
1385+
1386+
Context ('When validating resource/module {0}' -f $testCase.Folder) {
1387+
# If there are no test cases built, skip this test.
1388+
$skipTest_LocalizedKeys = -not $testCases_LocalizedKeys
1389+
1390+
It 'Should use the localized string key <LocalizedKey> from the localization resource file' -TestCases $testCases_LocalizedKeys -Skip:$skipTest_LocalizedKeys {
1391+
param
1392+
(
1393+
[Parameter()]
1394+
[System.String]
1395+
$LocalizedKey
1396+
)
1397+
1398+
$usedLocalizationKeys | Should -Contain $LocalizedKey -Because 'the key exists in the localized string resource file so it should also exist in the code'
1399+
}
1400+
1401+
# If there are no test cases built, skip this test.
1402+
$skipTest_UsedLocalizedKeys = -not $testCases_UsedLocalizedKeys
1403+
1404+
It 'Should not be missing the localized string key <LocalizedKey> from the localization resource file' -TestCases $testCases_UsedLocalizedKeys -Skip:$skipTest_UsedLocalizedKeys {
1405+
param
1406+
(
1407+
[Parameter()]
1408+
[System.String]
1409+
$LocalizedKey
1410+
)
1411+
1412+
$englishLocalizedStrings.Keys | Should -Contain $LocalizedKey -Because 'the key is used in the resource/module script file so it should also exist in the localized string resource files'
1413+
}
1414+
}
1415+
}
1416+
}
1417+
1418+
Context 'When a resource or module is localized to other languages' {
1419+
BeforeAll {
1420+
$otherLanguagesToTest = @()
1421+
1422+
foreach ($folder in $allFolders)
1423+
{
1424+
<#
1425+
Get all localization folders except the en-US.
1426+
We want all regardless of casing.
1427+
#>
1428+
$localizationFolders = Get-ChildItem -Path $folder.FullName -Directory -Filter '*-*' |
1429+
Where-Object -FilterScript {
1430+
$_.Name -ne 'en-US'
1431+
}
1432+
1433+
foreach ($localizationFolder in $localizationFolders)
1434+
{
1435+
$otherLanguagesToTest += @{
1436+
Folder = $folder.Name
1437+
Path = $folder.FullName
1438+
LocalizationFolder = $localizationFolder.Name
1439+
}
1440+
}
1441+
}
1442+
}
1443+
1444+
# Only run these tests if there are test cases to be tested.
1445+
$skipTests = -not $otherLanguagesToTest
1446+
1447+
It 'Should have a localization string resource file for the resource/module <Folder> and localization folder <LocalizationFolder>' -TestCases $otherLanguagesToTest -Skip:$skipTests {
1448+
param
1449+
(
1450+
[Parameter()]
1451+
[System.String]
1452+
$Folder,
1453+
1454+
[Parameter()]
1455+
[System.String]
1456+
$Path,
1457+
1458+
[Parameter()]
1459+
[System.String]
1460+
$LocalizationFolder
1461+
)
1462+
1463+
$localizationResourceFilePath = Join-Path -Path (Join-Path -Path $Path -ChildPath $LocalizationFolder) -ChildPath "$Folder.strings.psd1"
1464+
1465+
Test-Path -Path $localizationResourceFilePath | Should -BeTrue -Because ('there must exist a string resource file in the localization folder {0}' -f $LocalizationFolder)
1466+
}
1467+
1468+
It 'Should have a localization folder with the correct casing for the resource/module <Folder> and localization folder <LocalizationFolder>' -TestCases $otherLanguagesToTest -Skip:$skipTests {
1469+
param
1470+
(
1471+
[Parameter()]
1472+
[System.String]
1473+
$Folder,
1474+
1475+
[Parameter()]
1476+
[System.String]
1477+
$Path,
1478+
1479+
[Parameter()]
1480+
[System.String]
1481+
$LocalizationFolder
1482+
)
1483+
1484+
$localizationFolderOnDisk = Get-ChildItem -Path $Path -Directory -Filter $LocalizationFolder
1485+
$localizationFolderOnDisk.Name | Should -MatchExactly '[a-z]{2}-[A-Z]{2}' -Because 'the localization folder must have the correct casing'
1486+
}
1487+
1488+
foreach ($testCase in $otherLanguagesToTest)
1489+
{
1490+
$testCases_CompareAgainstEnglishLocalizedKeys = @()
1491+
$testCases_MissingEnglishLocalizedKeys = @()
1492+
1493+
$sourceLocalizationFolderPath = Join-Path -Path $testCase.Path -ChildPath 'en-US'
1494+
1495+
Import-LocalizedData `
1496+
-BindingVariable 'englishLocalizedStrings' `
1497+
-FileName "$($testCase.Folder).strings.psd1" `
1498+
-BaseDirectory $sourceLocalizationFolderPath
1499+
1500+
$localizationFolderPath = Join-Path -Path $testCase.Path -ChildPath $testCase.LocalizationFolder
1501+
1502+
Import-LocalizedData `
1503+
-BindingVariable 'localizedStrings' `
1504+
-FileName "$($testCase.Folder).strings.psd1" `
1505+
-BaseDirectory $localizationFolderPath
1506+
1507+
foreach ($localizedKey in $englishLocalizedStrings.Keys)
1508+
{
1509+
$testCases_CompareAgainstEnglishLocalizedKeys += @{
1510+
LocalizationFolder = $testCase.LocalizationFolder
1511+
Folder = $testCase.Folder
1512+
LocalizedKey = $localizedKey
1513+
}
1514+
}
1515+
1516+
foreach ($localizedKey in $localizedStrings.Keys)
1517+
{
1518+
$testCases_MissingEnglishLocalizedKeys += @{
1519+
LocalizationFolder = $testCase.LocalizationFolder
1520+
Folder = $testCase.Folder
1521+
LocalizedKey = $localizedKey
1522+
}
1523+
}
1524+
1525+
Context ('When validating resource/module {0}' -f $testCase.Folder) {
1526+
It "Should have the english localization string key <LocalizedKey> in the localization resource file '<LocalizationFolder>\<Folder>.strings.psd1' for the resource/module <Folder>" -TestCases $testCases_CompareAgainstEnglishLocalizedKeys {
1527+
param
1528+
(
1529+
[Parameter()]
1530+
[System.String]
1531+
$LocalizedKey,
1532+
1533+
[Parameter()]
1534+
[System.String]
1535+
$Folder,
1536+
1537+
[Parameter()]
1538+
[System.String]
1539+
$LocalizationFolder
1540+
)
1541+
1542+
$localizedStrings.Keys | Should -Contain $LocalizedKey -Because 'the key exists in the en-US localization resource file so the key should also exist in this language file'
1543+
} -ErrorVariable itBlockError
1544+
1545+
<#
1546+
If the It-block did not pass the test, output the a text
1547+
explaining how to resolve the issue.
1548+
#>
1549+
if ($itBlockError.Count -ne 0)
1550+
{
1551+
$message = @"
1552+
If you cannot translate the english string in the localized file,
1553+
then please just add the en-US localization string key together
1554+
with the en-US text string.
1555+
"@
1556+
1557+
Write-Host -BackgroundColor Yellow -ForegroundColor Black -Object $message
1558+
Write-Host -ForegroundColor White -Object ''
1559+
}
1560+
1561+
It "Should not be missing the localization string key <LocalizedKey> from the english resource file for the resource/module <Folder>" -TestCases $testCases_MissingEnglishLocalizedKeys {
1562+
param
1563+
(
1564+
[Parameter()]
1565+
[System.String]
1566+
$LocalizedKey,
1567+
1568+
[Parameter()]
1569+
[System.String]
1570+
$Folder,
1571+
1572+
[Parameter()]
1573+
[System.String]
1574+
$LocalizationFolder
1575+
)
1576+
1577+
$englishLocalizedStrings.Keys | Should -Contain $LocalizedKey -Because ('the key exists in the resource file for the location folder {0} so it should also exist in the en-US string resource file' -f $LocalizationFolder)
1578+
}
1579+
}
1580+
}
1581+
}
1582+
}
1583+
}

0 commit comments

Comments
 (0)