Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions mcp-server/AccUnit.McpServer/TestClassImportService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public TestClassImportService(AccessSession session)
_session = session ?? throw new ArgumentNullException(nameof(session));
}

/// <summary>VBComponents.Import reads the system ANSI code page (Encoding.Default on .NET Framework).</summary>
/// <summary>VBComponents.Import reads the system ANSI code page.</summary>
private static Encoding AnsiEncoding
{
get { return Encoding.Default; }
get { return VbaSourceFile.AnsiEncoding; }
}

public TestClassImportResult ImportTestClasses(string fileNameFilter, string importPath)
Expand Down Expand Up @@ -151,22 +151,26 @@ private static List<TestModuleSourceFile> SelectImportableFiles(VBProject vbProj

private static void Import(TestClassManager testClassManager, List<TestModuleSourceFile> 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);
}
}

Expand Down
50 changes: 16 additions & 34 deletions mcp-server/AccUnit.McpServer/TestModuleSourceFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using AccessCodeLib.Common.VBIDETools;

namespace AccessCodeLib.AccUnit.McpServer
{
/// <summary>
/// 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.
/// </summary>
public sealed class TestModuleSourceFile
{
Expand All @@ -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)
{
Expand All @@ -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));
}

/// <summary>UTF-8 with or without BOM if the bytes are valid UTF-8, otherwise the ANSI code page.</summary>
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));
}

/// <returns>null if the file can be imported, otherwise the reason why not.</returns>
Expand All @@ -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;
}

/// <summary>Writes the source in the ANSI code page with CRLF line endings, as the VBA editor exports it.</summary>
public void WriteAnsiCopy(string directory, Encoding ansiEncoding)
/// <summary>
/// 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.
/// </summary>
/// <returns>Path of the copy (same file name).</returns>
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;
}

/// <summary>
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}
}
3 changes: 2 additions & 1 deletion mcp-server/AccUnit.McpServer/TestTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 5 additions & 4 deletions mcp-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
(`<database folder>\Tests\<database name>` 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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;

Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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"));
}
}
}