From 5c7c9f4a126fce856345e8c44147f017e9c19a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20P=C3=B6tzl?= Date: Tue, 17 Nov 2026 16:40:04 +0100 Subject: [PATCH] Use core VbaSourceFile in MCP server import Reading, encoding check and ANSI conversion of test class files now use VbaSourceFile of the AccUnit core. The import copy is written as UTF-8 with BOM; the core converts it for VBComponents.Import. Export docs name the ImportExportEncoding setting. --- .../TestClassImportService.cs | 24 +++++---- .../AccUnit.McpServer/TestModuleSourceFile.cs | 50 ++++++------------- mcp-server/AccUnit.McpServer/TestTools.cs | 3 +- mcp-server/README.md | 9 ++-- .../TestModuleSourceFileTests.cs | 42 +++++++--------- 5 files changed, 54 insertions(+), 74 deletions(-) diff --git a/mcp-server/AccUnit.McpServer/TestClassImportService.cs b/mcp-server/AccUnit.McpServer/TestClassImportService.cs index 483dcd9..129b801 100644 --- a/mcp-server/AccUnit.McpServer/TestClassImportService.cs +++ b/mcp-server/AccUnit.McpServer/TestClassImportService.cs @@ -28,10 +28,10 @@ public TestClassImportService(AccessSession session) _session = session ?? throw new ArgumentNullException(nameof(session)); } - /// VBComponents.Import reads the system ANSI code page (Encoding.Default on .NET Framework). + /// VBComponents.Import reads the system ANSI code page. private static Encoding AnsiEncoding { - get { return Encoding.Default; } + get { return VbaSourceFile.AnsiEncoding; } } public TestClassImportResult ImportTestClasses(string fileNameFilter, string importPath) @@ -151,22 +151,26 @@ private static List SelectImportableFiles(VBProject vbProj private static void Import(TestClassManager testClassManager, List files, TestClassImportResult result) { - var ansiDirectory = Path.Combine(Path.GetTempPath(), "AccUnitMcpServer", "Import-" + Guid.NewGuid().ToString("N")); - Directory.CreateDirectory(ansiDirectory); + // Copies with export header, so code without header (e.g. the draft) is imported as class module; + // the core converts them to the ANSI code page. + var importDirectory = Path.Combine(Path.GetTempPath(), "AccUnitMcpServer", "Import-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(importDirectory); try { - foreach (var file in files) - file.WriteAnsiCopy(ansiDirectory, AnsiEncoding); + var copies = files.Select(file => new CodeModuleInfo(new FileInfo(file.WriteImportCopy(importDirectory)))).ToList(); // Same import as the VBE add-in: an existing module with the same name is replaced. - testClassManager.ImportTestComponents( - files.Select(file => new CodeModuleInfo(new FileInfo(Path.Combine(ansiDirectory, Path.GetFileName(file.FilePath))))), - true); + testClassManager.ImportTestComponents(copies, true); result.ImportedModules.AddRange(files.Select(file => file.ModuleName)); } + catch (UnsupportedSourceCharactersException ex) + { + // Not expected after the check per file; the core imports nothing in this case. + throw new DatabaseSessionException(ex.Message, ex); + } finally { - Directory.Delete(ansiDirectory, true); + Directory.Delete(importDirectory, true); } } diff --git a/mcp-server/AccUnit.McpServer/TestModuleSourceFile.cs b/mcp-server/AccUnit.McpServer/TestModuleSourceFile.cs index abf0205..e74c4e0 100644 --- a/mcp-server/AccUnit.McpServer/TestModuleSourceFile.cs +++ b/mcp-server/AccUnit.McpServer/TestModuleSourceFile.cs @@ -3,12 +3,14 @@ using System.Linq; using System.Text; using System.Text.RegularExpressions; +using AccessCodeLib.Common.VBIDETools; namespace AccessCodeLib.AccUnit.McpServer { /// - /// A test class or test related module in a source file (VBA export format), checked before it is imported. - /// VBComponents.Import reads the ANSI code page, while source files of agents and msaccess-vcs are usually UTF-8. + /// A test class or test related module in a source file (VBA export format, UTF-8 or ANSI), checked before it is imported. + /// The encoding (VbaSourceFile) and the conversion to ANSI for VBComponents.Import are done by the AccUnit core; + /// this class adds the checks of the MCP server and the export header for code without header. /// public sealed class TestModuleSourceFile { @@ -24,7 +26,6 @@ public sealed class TestModuleSourceFile RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); private const string ClassModuleExtension = ".cls"; private static readonly string[] LineSeparators = { "\r\n", "\n" }; - private static readonly byte[] Utf8Preamble = Encoding.UTF8.GetPreamble(); private TestModuleSourceFile(string filePath, string sourceCode) { @@ -42,23 +43,7 @@ private TestModuleSourceFile(string filePath, string sourceCode) public static TestModuleSourceFile Read(string filePath, Encoding ansiEncoding) { - return new TestModuleSourceFile(filePath, Decode(File.ReadAllBytes(filePath), ansiEncoding)); - } - - /// UTF-8 with or without BOM if the bytes are valid UTF-8, otherwise the ANSI code page. - public static string Decode(byte[] bytes, Encoding ansiEncoding) - { - if (bytes.Take(Utf8Preamble.Length).SequenceEqual(Utf8Preamble)) - return Encoding.UTF8.GetString(bytes, Utf8Preamble.Length, bytes.Length - Utf8Preamble.Length); - - try - { - return new UTF8Encoding(false, true).GetString(bytes); - } - catch (DecoderFallbackException) - { - return ansiEncoding.GetString(bytes); - } + return new TestModuleSourceFile(filePath, VbaSourceFile.ReadAllText(filePath, ansiEncoding)); } /// null if the file can be imported, otherwise the reason why not. @@ -71,17 +56,24 @@ public string GetRejectReason(Encoding ansiEncoding) if (vbName != null && !string.Equals(vbName, ModuleName, StringComparison.OrdinalIgnoreCase)) return "The file name must match the module name (Attribute VB_Name = \"" + vbName + "\")."; - if (!CanBeEncoded(SourceCode, ansiEncoding)) + // Checked per file, so the other files are still imported (the core rejects the whole import). + if (!VbaSourceFile.CanBeEncoded(SourceCode, ansiEncoding)) return "The file contains characters that are not available in the ANSI code page " + ansiEncoding.WebName + " used by the VBA editor."; return null; } - /// Writes the source in the ANSI code page with CRLF line endings, as the VBA editor exports it. - public void WriteAnsiCopy(string directory, Encoding ansiEncoding) + /// + /// Writes the source as it is imported (with export header) in UTF-8 with BOM and CRLF line endings; + /// the core converts it to the ANSI code page for VBComponents.Import. + /// + /// Path of the copy (same file name). + public string WriteImportCopy(string directory) { - File.WriteAllText(Path.Combine(directory, Path.GetFileName(FilePath)), ToCrLf(GetImportSource()), ansiEncoding); + var copyPath = Path.Combine(directory, Path.GetFileName(FilePath)); + File.WriteAllText(copyPath, VbaSourceFile.ToCrLf(GetImportSource()), VbaSourceFile.Utf8BomEncoding); + return copyPath; } /// @@ -113,11 +105,6 @@ private static string GetClassExportHeader(string moduleName) "Attribute VB_Exposed = False\r\n"; } - public static string ToCrLf(string sourceCode) - { - return string.Join("\r\n", sourceCode.Split(LineSeparators, StringSplitOptions.None)); - } - public static bool HasTestModuleHeader(string sourceCode) { var headerLines = sourceCode.Split(LineSeparators, StringSplitOptions.None) @@ -130,10 +117,5 @@ public static string GetVbName(string sourceCode) var match = VbNameRegex.Match(sourceCode); return match.Success ? match.Groups[1].Value : null; } - - private static bool CanBeEncoded(string sourceCode, Encoding ansiEncoding) - { - return ansiEncoding.GetString(ansiEncoding.GetBytes(sourceCode)) == sourceCode; - } } } diff --git a/mcp-server/AccUnit.McpServer/TestTools.cs b/mcp-server/AccUnit.McpServer/TestTools.cs index eb87aa1..d854ecf 100644 --- a/mcp-server/AccUnit.McpServer/TestTools.cs +++ b/mcp-server/AccUnit.McpServer/TestTools.cs @@ -80,7 +80,8 @@ public TestClassImportResult ImportTestClasses( [McpServerTool(Name = "accunit_export_test_classes")] [Description("Exports test classes and test related modules of the open database to the AccUnit test folder (VBA export " + - "format, ANSI), e.g. to edit an existing test class and import it again with accunit_import_test_classes. " + + "format; encoding from the AccUnit setting ImportExportEncoding, default UTF-8 with BOM), e.g. to edit an existing " + + "test class and import it again with accunit_import_test_classes. " + "Only modules with 'AccUnit:TestClass or 'AccUnit:TestRelated are exported. An existing file is written over only " + "with overwrite = true and is kept as .bak. Nothing is written to the database.")] public ExportTestClassesResult ExportTestClasses( diff --git a/mcp-server/README.md b/mcp-server/README.md index ef5a7c2..02d6198 100644 --- a/mcp-server/README.md +++ b/mcp-server/README.md @@ -115,12 +115,13 @@ A larger example of row tests: [SqlToolsBuildCriteriaTests.cls](https://github.c (`%TEMP%\AccUnitMcpServer`). - **`accunit_import_test_classes`** writes only test classes and test related modules (header comment `'AccUnit:TestClass` or `'AccUnit:TestRelated`). An existing test module with the same name is replaced; a module that is - not a test module is never replaced. The file name must match the module name. The files may be UTF-8 or ANSI; they are - converted to the ANSI code page of the VBA editor (characters outside it are rejected), and code without export - header gets the header the VBA editor writes. Default folder: the AccUnit test folder + not a test module is never replaced. The file name must match the module name. The files may be UTF-8 or ANSI; the + AccUnit core converts them to the ANSI code page of the VBA editor (files with characters outside it are rejected), + and code without export header gets the header the VBA editor writes. Default folder: the AccUnit test folder (`\Tests\` or the folder of the AccUnit setting `ImportExportFolder`). - **`accunit_export_test_classes`** and **`accunit_remove_test_classes`** work only on test classes and test related - modules. They export to the AccUnit test folder; an existing file is written over only with `overwrite = true` (export) + modules. They export to the AccUnit test folder in the encoding of the AccUnit setting `ImportExportEncoding` + (default UTF-8 with BOM, or ANSI); an existing file is written over only with `overwrite = true` (export) or before removing, and is kept as `.bak`. Removing a test class also removes `AccUnit_TestClassFactory` (core behavior); `accunit_run_tests` adds it again. - **`accunit_remove_test_environment`**, **`accunit_import_test_classes`** and **`accunit_remove_test_classes`** save the diff --git a/mcp-server/Tests/AccUnit.McpServer.Tests/TestModuleSourceFileTests.cs b/mcp-server/Tests/AccUnit.McpServer.Tests/TestModuleSourceFileTests.cs index 2dd3903..fa13296 100644 --- a/mcp-server/Tests/AccUnit.McpServer.Tests/TestModuleSourceFileTests.cs +++ b/mcp-server/Tests/AccUnit.McpServer.Tests/TestModuleSourceFileTests.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using System.Text; using NUnit.Framework; @@ -37,24 +38,19 @@ private TestModuleSourceFile CreateFile(string fileName, string content, Encodin } [Test] - public void Decode_Utf8WithBom_RemovesBomAndKeepsUmlauts() + public void Read_AnsiFile_KeepsUmlauts() { - var bytes = new UTF8Encoding(true).GetPreamble(); - bytes = Concat(bytes, Encoding.UTF8.GetBytes("' Gr\u00F6\u00DFe")); + var file = CreateFile("modStringsTests.cls", TestClassCode + "' Gr\u00F6\u00DFe\r\n", Ansi); - Assert.That(TestModuleSourceFile.Decode(bytes, Ansi), Is.EqualTo("' Gr\u00F6\u00DFe")); + Assert.That(file.SourceCode, Does.EndWith("' Gr\u00F6\u00DFe\r\n")); } [Test] - public void Decode_Utf8WithoutBom_KeepsUmlauts() + public void Read_Utf8FileWithBom_KeepsUmlauts() { - Assert.That(TestModuleSourceFile.Decode(Encoding.UTF8.GetBytes("' Gr\u00F6\u00DFe"), Ansi), Is.EqualTo("' Gr\u00F6\u00DFe")); - } + var file = CreateFile("modStringsTests.cls", TestClassCode + "' Gr\u00F6\u00DFe\r\n", new UTF8Encoding(true)); - [Test] - public void Decode_Ansi_KeepsUmlauts() - { - Assert.That(TestModuleSourceFile.Decode(Ansi.GetBytes("' Gr\u00F6\u00DFe"), Ansi), Is.EqualTo("' Gr\u00F6\u00DFe")); + Assert.That(file.SourceCode, Is.EqualTo(TestClassCode + "' Gr\u00F6\u00DFe\r\n")); } [TestCase("'AccUnit:TestClass")] @@ -138,24 +134,20 @@ public void GetImportSource_StandardModuleWithoutVbName_AddsVbName() } [Test] - public void WriteAnsiCopy_Utf8WithLf_WritesAnsiWithCrLf() + public void WriteImportCopy_Utf8WithLfWithoutHeader_WritesUtf8BomWithHeaderAndCrLf() { - var file = CreateFile("modStringsTests.cls", ClassExportHeader.Replace("\r\n", "\n") + "'AccUnit:TestClass\n' Gr\u00F6\u00DFe\n", new UTF8Encoding(true)); - var targetDirectory = Path.Combine(_directory, "ansi"); + var file = CreateFile("modStringsTests.cls", "'AccUnit:TestClass\n' Gr\u00F6\u00DFe\n", Encoding.UTF8); + var targetDirectory = Path.Combine(_directory, "import"); Directory.CreateDirectory(targetDirectory); - file.WriteAnsiCopy(targetDirectory, Ansi); + var copyPath = file.WriteImportCopy(targetDirectory); - var bytes = File.ReadAllBytes(Path.Combine(targetDirectory, "modStringsTests.cls")); - Assert.That(bytes, Is.EqualTo(Ansi.GetBytes(ClassExportHeader + "'AccUnit:TestClass\r\n' Gr\u00F6\u00DFe\r\n"))); - } - - private static byte[] Concat(byte[] first, byte[] second) - { - var result = new byte[first.Length + second.Length]; - first.CopyTo(result, 0); - second.CopyTo(result, first.Length); - return result; + Assert.That(copyPath, Is.EqualTo(Path.Combine(targetDirectory, "modStringsTests.cls"))); + var bytes = File.ReadAllBytes(copyPath); + Assert.That(bytes.Take(3), Is.EqualTo(new UTF8Encoding(true).GetPreamble())); + var text = new UTF8Encoding(true).GetString(bytes, 3, bytes.Length - 3); + Assert.That(text, Does.StartWith("VERSION 1.0 CLASS\r\n")); + Assert.That(text, Does.EndWith("'AccUnit:TestClass\r\n' Gr\u00F6\u00DFe\r\n")); } } }