| 1 | using Cage.GuestClipboard; |
| 2 | using System.Text; |
| 3 | |
| 4 | var root = Path.Combine(Path.GetTempPath(), "cage-editor-preferences-" + Guid.NewGuid().ToString("N")); |
| 5 | var user = Path.Combine(root, "User"); |
| 6 | var file = Path.Combine(user, "settings.json"); |
| 7 | try |
| 8 | { |
| 9 | if (WindowsEditorPreferences.TrustDefault(root) != false) throw new Exception("new Cage preference was not initialized"); |
| 10 | var original = "{\n // retain this comment 中文\n \"editor.fontSize\": 19,\n}\n"; |
| 11 | File.WriteAllText(file, original); |
| 12 | if (WindowsEditorPreferences.TrustDefault(root) != false || !File.ReadAllText(file).Contains(original[1..], StringComparison.Ordinal)) throw new Exception("existing comments/settings were replaced"); |
| 13 | foreach (var encoding in new Encoding[] { new UTF8Encoding(true), Encoding.Unicode }) |
| 14 | { |
| 15 | File.WriteAllText(file, "{\"security.workspace.trust.enabled\":true,\"editor.fontSize\":19}", encoding); |
| 16 | var before = File.ReadAllBytes(file); |
| 17 | if (WindowsEditorPreferences.TrustDefault(root) != true || !File.ReadAllBytes(file).SequenceEqual(before)) throw new Exception("explicit user true or its encoding changed"); |
| 18 | } |
| 19 | File.WriteAllText(file, "{\"security.workspace.trust.enabled\":null}"); |
| 20 | if (WindowsEditorPreferences.TrustDefault(root) is not null) throw new Exception("invalid explicit preference was overwritten"); |
| 21 | File.WriteAllText(file, "{malformed user file}"); |
| 22 | if (WindowsEditorPreferences.TrustDefault(root) is not null || File.ReadAllText(file) != "{malformed user file}") throw new Exception("malformed settings were replaced"); |
| 23 | Console.WriteLine("Cage editor preference initialization/preservation passed."); |
| 24 | } |
| 25 | finally { Directory.Delete(root, true); } |