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
1 change: 1 addition & 0 deletions source/AccUnit/AccessCodeLib.AccUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
<Compile Include="Interfaces\ITestRunner.cs" />
<Compile Include="Interfaces\ITestSuite.cs" />
<Compile Include="Interfaces\ITestSummary.cs" />
<Compile Include="Interfaces\ImportExportEncoding.cs" />
<Compile Include="Interfaces\ResetMode.cs" />
<Compile Include="Interfaces\RunState.cs" />
<Compile Include="Interfaces\TestContextMethodNames.cs" />
Expand Down
100 changes: 82 additions & 18 deletions source/AccUnit/Configuration/TestClassManager.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using AccessCodeLib.Common.Tools.Logging;
using AccessCodeLib.AccUnit.Interfaces;
using AccessCodeLib.Common.Tools.Logging;
using AccessCodeLib.Common.VBIDETools;
using AccessCodeLib.Common.VBIDETools.Templates;
using Microsoft.Vbe.Interop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace AccessCodeLib.AccUnit.Configuration
Expand Down Expand Up @@ -53,12 +55,23 @@ public string ExportTestClass(string name)

private string ExportTestClass(string name, string exportdirectory)
{
return CodeModuleManager.Export(name, exportdirectory);
return CodeModuleManager.Export(name, exportdirectory, ExportFileEncoding);
}

public string ExportTestClass(VBComponent component)
{
return CodeModuleManager.Export(component, ExportDirectory);
return CodeModuleManager.Export(component, ExportDirectory, ExportFileEncoding);
}

/// <summary>Encoding of exported .cls and .bas files (setting ImportExportEncoding); null keeps the ANSI file of the VBA editor.</summary>
private static Encoding ExportFileEncoding
{
get
{
return TestSuiteUserSettings.Current.ImportExportEncoding == ImportExportEncoding.Ansi
? null
: VbaSourceFile.Utf8BomEncoding;
}
}

public void RemoveTestClass(string name, bool export = true)
Expand Down Expand Up @@ -98,7 +111,7 @@ private void RemoveVbComponent(string name, bool export, string exportdirectory)
{
if (export)
{
CodeModuleManager.ExportAndRemove(name, exportdirectory);
CodeModuleManager.ExportAndRemove(name, exportdirectory, ExportFileEncoding);
}
else
{
Expand Down Expand Up @@ -261,15 +274,76 @@ public void ImportTestComponents(string fileNameFilter = null, string importPath
ImportTestComponents(importComponents, overwriteExistingComponent);
}

/// <exception cref="UnsupportedSourceCharactersException">
/// A .cls or .bas file contains characters that are not available in the ANSI code page of the VBA editor; nothing is imported.
/// </exception>
public void ImportTestComponents(IEnumerable<CodeModuleInfo> list, bool overwriteExistingComponent = false)
{
foreach (var m in list)
var modules = list.ToList();
ThrowIfSourceFilesCannotBeImported(modules);

string ansiDirectory = null;
try
{
if (overwriteExistingComponent)
foreach (var m in modules)
{
RemoveComponentIfExists(m);
if (overwriteExistingComponent)
{
RemoveComponentIfExists(m);
}

if (m.ComponentType == vbext_ComponentType.vbext_ct_Document)
{
ImportOfficeObject(new FileInfo(m.FileName));
}
else
{
if (ansiDirectory is null)
{
ansiDirectory = CreateTempImportDirectory();
}
// VBComponents.Import reads the ANSI code page: import an ANSI copy of the (UTF-8 or ANSI) source file
var ansiFile = VbaSourceFile.WriteAnsiCopy(m.FileName, ansiDirectory, VbaSourceFile.AnsiEncoding);
CodeModuleManager.Generator.Add(new FileInfo(ansiFile));
}
}
}
finally
{
if (ansiDirectory != null)
{
DeleteTempImportDirectory(ansiDirectory);
}
ImportTestComponent(new FileInfo(m.FileName), m.ComponentType);
}
}

private static void ThrowIfSourceFilesCannotBeImported(IEnumerable<CodeModuleInfo> modules)
{
var sourceFiles = modules.Where(m => m.ComponentType != vbext_ComponentType.vbext_ct_Document)
.Select(m => m.FileName);
var rejectedFiles = VbaSourceFile.GetFilesWithUnsupportedCharacters(sourceFiles, VbaSourceFile.AnsiEncoding).ToList();
if (rejectedFiles.Count > 0)
{
throw new UnsupportedSourceCharactersException(rejectedFiles, VbaSourceFile.AnsiEncoding);
}
}

private static string CreateTempImportDirectory()
{
var directory = Path.Combine(Path.GetTempPath(), "AccUnit", "Import-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(directory);
return directory;
}

private static void DeleteTempImportDirectory(string directory)
{
try
{
Directory.Delete(directory, true);
}
catch (Exception ex)
{
Logger.Log(ex);
}
}

Expand All @@ -293,16 +367,6 @@ private void RemoveComponentIfExists(CodeModuleInfo codeModuleInfo)
}


private void ImportTestComponent(FileInfo importFile, vbext_ComponentType type)
{
if (type == vbext_ComponentType.vbext_ct_Document)
ImportOfficeObject(importFile);
else
{
CodeModuleManager.Generator.Add(importFile);
}
}

private void ImportOfficeObject(FileSystemInfo importFile)
{
if (!(ApplicationHelper is AccessApplicationHelper accessApplication))
Expand Down
14 changes: 13 additions & 1 deletion source/AccUnit/Configuration/UserSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AccessCodeLib.AccUnit.Properties;
using AccessCodeLib.AccUnit.Interfaces;
using AccessCodeLib.AccUnit.Properties;
using AccessCodeLib.AccUnit.Tools.Templates;
using AccessCodeLib.Common.Tools.Logging;
using System;
Expand All @@ -19,6 +20,8 @@ public interface IUserSettings
string TestMethodTemplate { get; set; }

void Save();

ImportExportEncoding ImportExportEncoding { get; set; }
}

public class UserSettings : IUserSettings
Expand Down Expand Up @@ -126,6 +129,15 @@ public string ImportExportFolder
set { _testSuiteUserSettings.ImportExportFolder = value.TrimEnd('\\', ' ').TrimStart(); }
}

[Category("Import/Export")]
[DefaultValue(ImportExportEncoding.Utf8Bom)]
[Description("Encoding of exported test classes (.cls, .bas): Utf8Bom (UTF-8 with BOM) or Ansi (code page of the VBA editor).\nThe import detects the encoding.")]
public ImportExportEncoding ImportExportEncoding
{
get { return _testSuiteUserSettings.ImportExportEncoding; }
set { _testSuiteUserSettings.ImportExportEncoding = value; }
}

[Category("Templates")]
[DefaultValue(@"%APPDATA%\AccessCodeLib\AccUnit\Templates")]
[Description("Location of template files")]
Expand Down
9 changes: 8 additions & 1 deletion source/AccUnit/Integration/TestSuiteUserSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AccessCodeLib.AccUnit.Properties;
using AccessCodeLib.AccUnit.Interfaces;
using AccessCodeLib.AccUnit.Properties;
using AccessCodeLib.Common.Tools.Logging;
using System;
using System.ComponentModel;
Expand Down Expand Up @@ -47,6 +48,7 @@ public TestSuiteUserSettings Clone()
var clone = new TestSuiteUserSettings
{
ImportExportFolder = ImportExportFolder,
ImportExportEncoding = ImportExportEncoding,
SeparatorChar = SeparatorChar,
SeparatorMaxLength = SeparatorMaxLength
};
Expand All @@ -60,6 +62,7 @@ private void Load()
using (new BlockLogger())
{
ImportExportFolder = Settings.Default.ImportExportFolder;
ImportExportEncoding = Settings.Default.ImportExportEncoding;
SeparatorMaxLength = Settings.Default.SeparatorMaxLength;
SeparatorChar = Settings.Default.SeparatorChar;
}
Expand All @@ -68,6 +71,7 @@ private void Load()
public void Save()
{
Settings.Default.ImportExportFolder = ImportExportFolder;
Settings.Default.ImportExportEncoding = ImportExportEncoding;
Settings.Default.SeparatorMaxLength = SeparatorMaxLength;
Settings.Default.SeparatorChar = SeparatorChar;
Settings.Default.Save();
Expand All @@ -90,6 +94,9 @@ public string ImportExportFolder
}
}

/// <summary>Encoding of exported .cls and .bas files; the import detects the encoding.</summary>
public ImportExportEncoding ImportExportEncoding { get; set; }

[Category("Text output")]
[DefaultValue(60)]
[Description("")]
Expand Down
9 changes: 8 additions & 1 deletion source/AccUnit/Integration/UserSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AccessCodeLib.AccUnit.Properties;
using AccessCodeLib.AccUnit.Interfaces;
using AccessCodeLib.AccUnit.Properties;
using AccessCodeLib.Common.Tools.Logging;
using System;
using System.ComponentModel;
Expand Down Expand Up @@ -47,6 +48,7 @@ public UserSettings Clone()
var clone = new UserSettings
{
ImportExportFolder = ImportExportFolder,
ImportExportEncoding = ImportExportEncoding,
SeparatorChar = SeparatorChar,
SeparatorMaxLength = SeparatorMaxLength
};
Expand All @@ -60,6 +62,7 @@ private void Load()
using (new BlockLogger())
{
ImportExportFolder = Settings.Default.ImportExportFolder;
ImportExportEncoding = Settings.Default.ImportExportEncoding;
SeparatorMaxLength = Settings.Default.SeparatorMaxLength;
SeparatorChar = Settings.Default.SeparatorChar;
}
Expand All @@ -68,6 +71,7 @@ private void Load()
public void Save()
{
Settings.Default.ImportExportFolder = ImportExportFolder;
Settings.Default.ImportExportEncoding = ImportExportEncoding;
Settings.Default.SeparatorMaxLength = SeparatorMaxLength;
Settings.Default.SeparatorChar = SeparatorChar;
Settings.Default.Save();
Expand All @@ -90,6 +94,9 @@ public string ImportExportFolder
}
}

/// <summary>Encoding of exported .cls and .bas files; the import detects the encoding.</summary>
public ImportExportEncoding ImportExportEncoding { get; set; }

[Category("Text output")]
[DefaultValue(60)]
[Description("")]
Expand Down
16 changes: 16 additions & 0 deletions source/AccUnit/Interfaces/ImportExportEncoding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Runtime.InteropServices;

namespace AccessCodeLib.AccUnit.Interfaces
{
/// <summary>
/// Encoding of exported test class files (.cls, .bas).
/// The import always detects the encoding (UTF-8 with or without BOM, otherwise ANSI).
/// </summary>
[ComVisible(true)]
[Guid("FA0E0FD9-F0F3-40CB-9D8D-D7FD4502A8C0")]
public enum ImportExportEncoding
{
Utf8Bom = 0,
Ansi = 1
}
}
12 changes: 12 additions & 0 deletions source/AccUnit/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions source/AccUnit/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<Setting Name="ImportExportFolder" Type="System.String" Scope="User">
<Value Profile="(Default)">%APPFOLDER%\Tests\%APPNAME%</Value>
</Setting>
<Setting Name="ImportExportEncoding" Type="AccessCodeLib.AccUnit.Interfaces.ImportExportEncoding" Scope="User">
<Value Profile="(Default)">Utf8Bom</Value>
</Setting>
<Setting Name="TemplateFolder" Type="System.String" Scope="User">
<Value Profile="(Default)">%APPDATA%\AccessCodeLib\AccUnit\Templates</Value>
</Setting>
Expand Down
3 changes: 3 additions & 0 deletions source/AccUnit/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<setting name="ImportExportFolder" serializeAs="String">
<value>%APPFOLDER%\Tests\%APPNAME%</value>
</setting>
<setting name="ImportExportEncoding" serializeAs="String">
<value>Utf8Bom</value>
</setting>
<setting name="TemplateFolder" serializeAs="String">
<value>%APPDATA%\AccessCodeLib\AccUnit\Templates</value>
</setting>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@
<Compile Include="CodeModuleGenerator.cs" />
<Compile Include="CodeModuleInfo.cs" />
<Compile Include="CodeModuleContainer.cs" />
<Compile Include="UnsupportedSourceCharactersException.cs" />
<Compile Include="VbaSourceFile.cs" />
<Compile Include="CodeModuleMember.cs" />
<Compile Include="CodeModuleMemberList.cs" />
<Compile Include="CodeModuleReader.cs" />
Expand Down
36 changes: 35 additions & 1 deletion source/Common/Common.VBIDETools/CodeModuleContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Vbe.Interop;
using System;
using System.Linq;
using System.Text;

namespace AccessCodeLib.Common.VBIDETools
{
Expand Down Expand Up @@ -68,13 +69,35 @@ public string Export(string name, string exportfolder)
return Export(c, exportfolder);
}

public string Export(string name, string exportfolder, Encoding fileEncoding)
{
var c = _vbProject.VBComponents.Item(name);
return Export(c, exportfolder, fileEncoding);
}

public string Export(VBComponent component, string exportfolder)
{
return Export(component, exportfolder, null);
}

/// <param name="fileEncoding">Encoding of .cls and .bas files; null keeps the ANSI file of the VBA editor.</param>
public string Export(VBComponent component, string exportfolder, Encoding fileEncoding)
{
var fileName = $"{exportfolder.TrimEnd(' ', '\\')}\\{component.Name}{GetVbComponentTypeFileExtension(component.Type)}";
component.Export(fileName);
if (fileEncoding != null && IsConvertibleComponentType(component.Type))
{
VbaSourceFile.ConvertExportedFile(fileName, fileEncoding, VbaSourceFile.AnsiEncoding);
}
return fileName;
}

private static bool IsConvertibleComponentType(vbext_ComponentType componenttype)
{
return componenttype == vbext_ComponentType.vbext_ct_ClassModule ||
componenttype == vbext_ComponentType.vbext_ct_StdModule;
}

private static string GetVbComponentTypeFileExtension(vbext_ComponentType componenttype)
{
string fileExtension;
Expand Down Expand Up @@ -107,7 +130,18 @@ public string ExportAndRemove(string name, string exportfolder)

public string ExportAndRemove(VBComponent component, string exportfolder)
{
var fileName = Export(component, exportfolder);
return ExportAndRemove(component, exportfolder, null);
}

public string ExportAndRemove(string name, string exportfolder, Encoding fileEncoding)
{
var c = _vbProject.VBComponents.Item(name);
return ExportAndRemove(c, exportfolder, fileEncoding);
}

public string ExportAndRemove(VBComponent component, string exportfolder, Encoding fileEncoding)
{
var fileName = Export(component, exportfolder, fileEncoding);
Remove(component);
return fileName;
}
Expand Down
Loading