|
| 1 | +// Copyright (c) 2020 SIL International |
| 2 | +// This software is licensed under the MIT License (http://opensource.org/licenses/MIT) |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Threading; |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +namespace SIL.BuildTasks.Tests |
| 10 | +{ |
| 11 | + [TestFixture] |
| 12 | + public class NormalizeLocalesTests |
| 13 | + { |
| 14 | + private NormalizeLocales _task; |
| 15 | + private string _testDir; |
| 16 | + |
| 17 | + [SetUp] |
| 18 | + public void TestSetup() |
| 19 | + { |
| 20 | + _testDir = Path.Combine(Path.GetTempPath(), GetType().Name); |
| 21 | + _task = new NormalizeLocales { BuildEngine = new MockBuildEngine(), L10nsDirectory = _testDir }; |
| 22 | + |
| 23 | + RecreateDirectory(_testDir); |
| 24 | + } |
| 25 | + |
| 26 | + [TearDown] |
| 27 | + public void TestTeardown() |
| 28 | + { |
| 29 | + try |
| 30 | + { |
| 31 | + Directory.Delete(_testDir, true); |
| 32 | + } |
| 33 | + catch |
| 34 | + { |
| 35 | + Debug.WriteLine("Test could not clean up data directories."); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + [Test] |
| 40 | + public void DoesntCrashWhenNoNameChange() |
| 41 | + { |
| 42 | + FileSystemSetup(new[] { "de", "en-US" }); |
| 43 | + |
| 44 | + _task.Execute(); |
| 45 | + |
| 46 | + // Verify that the already-normalized locale is still normalized |
| 47 | + VerifyLocale("de", "never-existed"); |
| 48 | + // Verify that the locale with a region has been normalized |
| 49 | + VerifyLocale("en", "en-US"); |
| 50 | + } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public void Works() |
| 54 | + { |
| 55 | + FileSystemSetup(new[] { "de-DE", "en-US", "zh-CN" }); |
| 56 | + |
| 57 | + _task.Execute(); |
| 58 | + |
| 59 | + // Verify that normal languages have no country codes |
| 60 | + VerifyLocale("de", "de-DE"); |
| 61 | + VerifyLocale("en", "en-US"); |
| 62 | + |
| 63 | + // Verify that Chinese has the country code and that there is no regionless Chinese. |
| 64 | + VerifyLocale("zh-CN", "zh"); |
| 65 | + } |
| 66 | + |
| 67 | + private void FileSystemSetup(string[] locales) |
| 68 | + { |
| 69 | + foreach (var locale in locales) |
| 70 | + { |
| 71 | + var localeDir = Path.Combine(_testDir, locale); |
| 72 | + Directory.CreateDirectory(localeDir); |
| 73 | + File.WriteAllText(Path.Combine(localeDir, $"strings-{locale}.xml"), "some strings"); |
| 74 | + File.WriteAllText(Path.Combine(localeDir, $"Palaso.{locale}.xlf"), "pretend this is xliff"); |
| 75 | + var projectDir = Path.Combine(localeDir, "someProject"); |
| 76 | + Directory.CreateDirectory(projectDir); |
| 77 | + File.WriteAllText(Path.Combine(projectDir, $"SomeFile.{locale}.resx"), "contents"); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private void VerifyLocale(string expected, string not) |
| 82 | + { |
| 83 | + var localeDir = Path.Combine(_testDir, expected); |
| 84 | + AssertDirExists(localeDir); |
| 85 | + AssertDirDoesNotExist(Path.Combine(_testDir, not)); |
| 86 | + |
| 87 | + AssertFileExists(Path.Combine(localeDir, $"strings-{expected}.xml")); |
| 88 | + AssertFileDoesNotExist(Path.Combine(localeDir, $"strings-{not}.xml")); |
| 89 | + AssertFileExists(Path.Combine(localeDir, $"Palaso.{expected}.xlf")); |
| 90 | + AssertFileDoesNotExist(Path.Combine(localeDir, $"Palaso.{not}.xlf")); |
| 91 | + AssertFileExists(Path.Combine(localeDir, "someProject", $"SomeFile.{expected}.resx")); |
| 92 | + AssertFileDoesNotExist(Path.Combine(localeDir, "someProject", $"SomeFile.{not}.resx")); |
| 93 | + } |
| 94 | + |
| 95 | + private static void AssertDirExists(string path) |
| 96 | + { |
| 97 | + Assert.That(Directory.Exists(path), $"Expected the directory {path} to exist, but it did not."); |
| 98 | + } |
| 99 | + |
| 100 | + private static void AssertDirDoesNotExist(string path) |
| 101 | + { |
| 102 | + Assert.That(!Directory.Exists(path), $"Expected the directory {path} not to exist, but it did."); |
| 103 | + } |
| 104 | + |
| 105 | + private static void AssertFileExists(string path) |
| 106 | + { |
| 107 | + Assert.That(File.Exists(path), $"Expected the file {path} to exist, but it did not."); |
| 108 | + } |
| 109 | + |
| 110 | + private static void AssertFileDoesNotExist(string path) |
| 111 | + { |
| 112 | + Assert.That(!File.Exists(path), $"Expected the file {path} not to exist, but it did."); |
| 113 | + } |
| 114 | + public static void RecreateDirectory(string path) |
| 115 | + { |
| 116 | + if (Directory.Exists(path)) |
| 117 | + { |
| 118 | + try |
| 119 | + { |
| 120 | + Directory.Delete(path, true); |
| 121 | + Thread.Sleep(1000); // wait for the filesystem to finish deleting |
| 122 | + } |
| 123 | + catch |
| 124 | + { |
| 125 | + Assert.Fail("Couldn't delete old test data from aborted runs"); |
| 126 | + } |
| 127 | + } |
| 128 | + Directory.CreateDirectory(path); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments