Skip to content

Commit c458e83

Browse files
Convert to C#
Ensure tests capture successful conversion
1 parent d466f46 commit c458e83

15 files changed

Lines changed: 463 additions & 356 deletions
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System.Diagnostics;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using TextFileConvert;
7+
8+
9+
10+
namespace TestLineEndingConversion;
11+
12+
[TestClass]
13+
public class IntegrationTesting
14+
{
15+
private const string TestunixTxt = "testunix.txt";
16+
private const string TestdosTxt = "testdos.txt";
17+
18+
[TestMethod]
19+
public async Task TestDos2Ux()
20+
{
21+
// Where am I?
22+
Debug.WriteLine(Directory.GetCurrentDirectory());
23+
if(File.Exists(TestunixTxt))
24+
File.Delete(TestunixTxt);
25+
26+
// ensure we start well
27+
var precondition = await File.ReadAllTextAsync("dos.txt");
28+
Assert.IsTrue(precondition.Contains("\r\n"));
29+
30+
// Do Conversion
31+
var exitCode = await ConvertLineEndings.Dos2Ux("dos.txt", TestunixTxt);
32+
33+
// Zero is success
34+
Assert.AreEqual(exitCode, 0);
35+
36+
// Compare the test file to the artifact file
37+
var result = await File.ReadAllTextAsync(TestunixTxt);
38+
Assert.IsFalse(result.Contains("\r\n"));
39+
Assert.AreEqual(await File.ReadAllTextAsync("unix.txt"), result);
40+
41+
// Clean up
42+
File.Delete(TestunixTxt);
43+
}
44+
45+
[TestMethod]
46+
public async Task TestUx2Dos()
47+
{
48+
// Where am I?
49+
Debug.WriteLine(Directory.GetCurrentDirectory());
50+
51+
if(File.Exists(TestdosTxt))
52+
File.Delete(TestdosTxt);
53+
54+
// ensure we start well
55+
var precondition = await File.ReadAllTextAsync("unix.txt");
56+
Assert.IsFalse(precondition.Contains("\r\n"));
57+
58+
// Do Conversion
59+
int exitCode = await ConvertLineEndings.Ux2Dos("unix.txt", TestdosTxt);
60+
61+
// Zero is success
62+
Assert.AreEqual(exitCode, 0);
63+
64+
// Compare the test file to the artifact file
65+
var result = await File.ReadAllTextAsync(TestdosTxt);
66+
Assert.IsTrue(result.Contains("\r\n"));
67+
Assert.AreEqual(await File.ReadAllTextAsync("dos.txt"), result);
68+
69+
// Clean up
70+
File.Delete(TestdosTxt);
71+
}
72+
}

TestLineEndingConversion/IntegrationTesting.vb

Lines changed: 0 additions & 47 deletions
This file was deleted.

TestLineEndingConversion/TestLineEndingConversion.vbproj renamed to TestLineEndingConversion/TestLineEndingConversion.csproj

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
43
<RootNamespace>TestLineEndingConversion</RootNamespace>
5-
<TargetFramework>net5.0</TargetFramework>
6-
4+
<TargetFramework>net8.0</TargetFramework>
75
<IsPackable>false</IsPackable>
6+
<DefaultItemExcludes>$(DefaultItemExcludes);$(ProjectDir)**\*.vb</DefaultItemExcludes>
7+
<LangVersion>latest</LangVersion>
88
</PropertyGroup>
9-
109
<ItemGroup>
1110
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
1211
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
1312
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
1413
<PackageReference Include="coverlet.collector" Version="3.0.2" />
1514
</ItemGroup>
16-
1715
<ItemGroup>
18-
<ProjectReference Include="..\TextFileConvert\TextFileConvert.vbproj" />
16+
<ProjectReference Include="..\TextFileConvert\TextFileConvert.csproj" />
1917
</ItemGroup>
20-
2118
<ItemGroup>
2219
<None Update="dos.txt">
2320
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
@@ -29,5 +26,4 @@
2926
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3027
</None>
3128
</ItemGroup>
32-
33-
</Project>
29+
</Project>

TextFileConvert.sln

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.31613.86
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "TextFileConvert", "TextFileConvert\TextFileConvert.vbproj", "{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TextFileConvert", "TextFileConvert\TextFileConvert.csproj", "{74F197CF-6465-0FAB-0D1E-1549AB5569EB}"
77
EndProject
8-
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "dos2ux", "dos2ux\dos2ux.vbproj", "{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dos2ux", "dos2ux\dos2ux.csproj", "{183B492B-D612-00F6-38A4-41567E795BB0}"
99
EndProject
10-
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "ux2dos", "ux2dos\ux2dos.vbproj", "{C596EC73-1634-429E-A20C-CD73DB6D416C}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ux2dos", "ux2dos\ux2dos.csproj", "{77B26D18-DA6C-0D6F-2054-070DB9FA75CC}"
1111
EndProject
12-
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "TestLineEndingConversion", "TestLineEndingConversion\TestLineEndingConversion.vbproj", "{FC6A6D47-7C99-41D6-BD55-C6394BF804C4}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestLineEndingConversion", "TestLineEndingConversion\TestLineEndingConversion.csproj", "{4E4EEC2C-B0C1-0E27-3F0D-0C47296F3064}"
1313
EndProject
1414
Global
1515
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1616
Debug|Any CPU = Debug|Any CPU
1717
Release|Any CPU = Release|Any CPU
1818
EndGlobalSection
1919
GlobalSection(ProjectConfigurationPlatforms) = postSolution
20-
{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
23-
{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}.Release|Any CPU.Build.0 = Release|Any CPU
24-
{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25-
{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}.Debug|Any CPU.Build.0 = Debug|Any CPU
26-
{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}.Release|Any CPU.ActiveCfg = Release|Any CPU
27-
{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}.Release|Any CPU.Build.0 = Release|Any CPU
28-
{C596EC73-1634-429E-A20C-CD73DB6D416C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29-
{C596EC73-1634-429E-A20C-CD73DB6D416C}.Debug|Any CPU.Build.0 = Debug|Any CPU
30-
{C596EC73-1634-429E-A20C-CD73DB6D416C}.Release|Any CPU.ActiveCfg = Release|Any CPU
31-
{C596EC73-1634-429E-A20C-CD73DB6D416C}.Release|Any CPU.Build.0 = Release|Any CPU
32-
{FC6A6D47-7C99-41D6-BD55-C6394BF804C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33-
{FC6A6D47-7C99-41D6-BD55-C6394BF804C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
34-
{FC6A6D47-7C99-41D6-BD55-C6394BF804C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
35-
{FC6A6D47-7C99-41D6-BD55-C6394BF804C4}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{74F197CF-6465-0FAB-0D1E-1549AB5569EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{74F197CF-6465-0FAB-0D1E-1549AB5569EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{74F197CF-6465-0FAB-0D1E-1549AB5569EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{74F197CF-6465-0FAB-0D1E-1549AB5569EB}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{183B492B-D612-00F6-38A4-41567E795BB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{183B492B-D612-00F6-38A4-41567E795BB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{183B492B-D612-00F6-38A4-41567E795BB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{183B492B-D612-00F6-38A4-41567E795BB0}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{77B26D18-DA6C-0D6F-2054-070DB9FA75CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
29+
{77B26D18-DA6C-0D6F-2054-070DB9FA75CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
30+
{77B26D18-DA6C-0D6F-2054-070DB9FA75CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
31+
{77B26D18-DA6C-0D6F-2054-070DB9FA75CC}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{4E4EEC2C-B0C1-0E27-3F0D-0C47296F3064}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{4E4EEC2C-B0C1-0E27-3F0D-0C47296F3064}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{4E4EEC2C-B0C1-0E27-3F0D-0C47296F3064}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{4E4EEC2C-B0C1-0E27-3F0D-0C47296F3064}.Release|Any CPU.Build.0 = Release|Any CPU
3636
EndGlobalSection
3737
GlobalSection(SolutionProperties) = preSolution
3838
HideSolutionNode = FALSE

TextFileConvert/Common.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
6+
namespace TextFileConvert;
7+
8+
public static class Common
9+
{
10+
/// <summary>
11+
/// This is the command line help that is displayed when invalid parameters are given.
12+
/// </summary>
13+
/// <param name="programName"></param>
14+
public static void PrintUsage(string programName)
15+
{
16+
Console.WriteLine("""
17+
18+
NAME
19+
{0} - Text file format converter
20+
21+
SYNOPSIS
22+
{0} old-filename new-filename
23+
24+
DESCRIPTION
25+
{0} reads old-filename and writes out new-filename, converting line endings.
26+
27+
""", programName);
28+
}
29+
30+
/// <summary>
31+
/// Attempt to detect the encoding of a file.
32+
/// </summary>
33+
/// <param name="theFile">The file to get the encoding pattern from.</param>
34+
/// <returns>Encoding type, defaults to ASCII.</returns>
35+
public static async Task<Encoding> GetEncoding(FileStream theFile)
36+
{
37+
var bom = new byte[4];
38+
var count = await theFile.ReadAsync(bom, 0, 4);
39+
40+
// Detect BOM type
41+
if (count > 2 && bom[0] == 0x2B && bom[1] == 0x2F && bom[2] == 0x76)
42+
{
43+
return Encoding.UTF7;
44+
}
45+
if (count > 2 && bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF)
46+
{
47+
return Encoding.UTF8;
48+
}
49+
if (count > 1 && bom[0] == 0xFF && bom[1] == 0xFE)
50+
{
51+
return Encoding.Unicode;
52+
}
53+
if (count > 1 && bom[0] == 0xFE && bom[1] == 0xFF)
54+
{
55+
return Encoding.BigEndianUnicode;
56+
}
57+
if (count > 3 && bom[0] == 0 && bom[1] == 0 && bom[2] == 0xFE && bom[3] == 0xFF)
58+
{
59+
return Encoding.UTF32;
60+
}
61+
62+
// Default to
63+
return Encoding.ASCII;
64+
}
65+
66+
/// <summary>
67+
/// Detect when the file in the given path is a symbolic link.
68+
/// WARNING: Could have false positive for any file with a reparse point that is not a symbolic link.
69+
/// </summary>
70+
/// <param name="path">Full path to file to test.</param>
71+
/// <returns>True if Reparse Point is found.</returns>
72+
public static bool IsSymbolic(string path)
73+
{
74+
var pathInfo = new FileInfo(path);
75+
return pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint);
76+
}
77+
}

0 commit comments

Comments
 (0)