Skip to content

Commit ee06023

Browse files
author
A9G-Data-Droid
committed
Add Integration Tests
1 parent 9db1653 commit ee06023

29 files changed

Lines changed: 541 additions & 587 deletions
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Imports System.IO
2+
Imports Microsoft.VisualStudio.TestTools.UnitTesting
3+
Imports TextFileConvert
4+
5+
Namespace TestLineEndingConversion
6+
<TestClass>
7+
Public Class IntegrationTesting
8+
<TestMethod>
9+
Sub TestDos2Ux()
10+
' Where am I?
11+
Debug.WriteLine(Directory.GetCurrentDirectory())
12+
13+
' Do Conversion
14+
Dim exitCode As Integer = ConvertLineEndings.Dos2Ux("dos.txt", "testunix.txt").GetAwaiter().GetResult()
15+
16+
' Zero is success
17+
Assert.AreEqual(exitCode, 0)
18+
19+
' Compare the test file to the artifact file
20+
Assert.IsTrue(File.ReadAllBytes("unix.txt").SequenceEqual(File.ReadAllBytes("testunix.txt")))
21+
22+
' Clean up
23+
File.Delete("testunix.txt")
24+
End Sub
25+
26+
<TestMethod>
27+
Sub TestUx2Dos()
28+
' Where am I?
29+
Debug.WriteLine(Directory.GetCurrentDirectory())
30+
31+
' Do Conversion
32+
Dim exitCode As Integer = ConvertLineEndings.Ux2Dos("unix.txt", "testdos.txt").GetAwaiter().GetResult()
33+
34+
' Zero is success
35+
Assert.AreEqual(exitCode, 0)
36+
37+
' Compare the test file to the artifact file
38+
Assert.IsTrue(File.ReadAllBytes("dos.txt").SequenceEqual(File.ReadAllBytes("testdos.txt")))
39+
40+
' Clean up
41+
File.Delete("testdos.txt")
42+
End Sub
43+
End Class
44+
End Namespace
45+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<RootNamespace>TestLineEndingConversion</RootNamespace>
5+
<TargetFramework>net5.0</TargetFramework>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
12+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
13+
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
14+
<PackageReference Include="coverlet.collector" Version="3.0.2" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\TextFileConvert\TextFileConvert.vbproj" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<None Update="dos.txt">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
<None Update="mac.txt">
26+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
27+
</None>
28+
<None Update="unix.txt">
29+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
30+
</None>
31+
</ItemGroup>
32+
33+
</Project>

TestLineEndingConversion/dos.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
hello
2+
world
3+
4+
take
5+
a
6+
break

TestLineEndingConversion/mac.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
helloworldtakeabreak

TestLineEndingConversion/unix.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
hello
2+
world
3+
4+
take
5+
a
6+
break

TextFileConvert.sln

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27428.2037
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31613.86
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "TextFileConvert", "TextFileConvert\TextFileConvert.vbproj", "{C6D516A4-A83D-405A-8F46-DF37C9C25D4B}"
77
EndProject
88
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "dos2ux", "dos2ux\dos2ux.vbproj", "{AA1FC840-1A4A-4F07-BAFC-8B281CEE6F10}"
99
EndProject
10-
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "ux2dos", "ux2dos\ux2dos.vbproj", "{C596EC73-1634-429E-A20C-CD73DB6D416C}"
10+
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "ux2dos", "ux2dos\ux2dos.vbproj", "{C596EC73-1634-429E-A20C-CD73DB6D416C}"
11+
EndProject
12+
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "TestLineEndingConversion", "TestLineEndingConversion\TestLineEndingConversion.vbproj", "{FC6A6D47-7C99-41D6-BD55-C6394BF804C4}"
1113
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,6 +29,10 @@ Global
2729
{C596EC73-1634-429E-A20C-CD73DB6D416C}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{C596EC73-1634-429E-A20C-CD73DB6D416C}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{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
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

TextFileConvert/ConvertLineEndings.vb

Lines changed: 44 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,19 @@ Public Class ConvertLineEndings
4545
convertMode As TextConvertMode) As Task(Of Integer)
4646

4747
Try
48-
Using oldFileStream As New FileStream(originalFile, FileMode.Open)
49-
' Attempt to detect encoding
50-
Dim fileEncoding As Encoding = GetEncoding(oldFileStream)
51-
If fileEncoding Is Nothing Then Return 4
48+
' Do not attempt to work on symbolic links
49+
If IsSymbolic(originalFile) Then Return -1
50+
51+
Using oldFileStream As New FileStream(originalFile, FileMode.Open, FileAccess.Read)
52+
' Attempt to detect encoding we will use for reading and writing
53+
Dim fileEncoding As Encoding = Await GetEncoding(oldFileStream)
5254
Debug.Print(fileEncoding.ToString())
5355

54-
'Write the result out to a new file
56+
' Rewind stream
57+
oldFileStream.Position = 0
58+
Await oldFileStream.FlushAsync
59+
60+
' Reading and writing is done in this one line
5561
File.WriteAllText(newFile, Await GetConvertedText(oldFileStream, fileEncoding, convertMode), fileEncoding)
5662
End Using
5763
Catch ex As Exception
@@ -63,78 +69,42 @@ Public Class ConvertLineEndings
6369
End Function
6470

6571
Private Shared Async Function GetConvertedText(originalFile As FileStream, fileEncoding As Encoding, convertMode As TextConvertMode) As Task(Of String)
66-
Const cr As Char = ChrW(13)
67-
Const lf As Char = ChrW(10)
72+
Const CR As Char = ChrW(13)
73+
Const LF As Char = ChrW(10)
6874

69-
Dim convertedLines As StringBuilder = Nothing
70-
Try
71-
Using oldFile As New StreamReader(originalFile, fileEncoding, True)
72-
Do Until oldFile.EndOfStream ' Read through the whole file
73-
Dim readBuffer(0) As Char
74-
Dim readChars As Integer = Await oldFile.ReadAsync(readBuffer, 0, 1)
75-
If readChars >= 1 Then
76-
Select Case convertMode
77-
Case TextConvertMode.Dos2Ux
78-
If readBuffer(0) = cr AndAlso oldFile.Peek() = 10 Then
79-
' Strip out CR chars if followed by LF
80-
Await oldFile.ReadAsync(readBuffer, 0, 1)
81-
End If
82-
Case TextConvertMode.Ux2Dos
83-
If readBuffer(0) = cr AndAlso oldFile.Peek() = 10 Then
84-
ReDim Preserve readBuffer(1)
85-
' This is a DOS line ending, keep it.
86-
Dim tempBuffer(1) As Char
87-
Await oldFile.ReadAsync(tempBuffer, 0, 1)
88-
readBuffer(1) = tempBuffer(0)
89-
ElseIf readBuffer(0) = lf Then
90-
ReDim readBuffer(1)
91-
' Add preceeding CR
92-
readBuffer(0) = cr
93-
readBuffer(1) = lf
94-
End If
95-
End Select
75+
Dim convertedLines As New StringBuilder
76+
Using oldFile As New StreamReader(originalFile, fileEncoding, True)
77+
Do Until oldFile.EndOfStream ' Read through the whole file
78+
Dim readBuffer(0) As Char
79+
Dim readChars As Integer = Await oldFile.ReadAsync(readBuffer, 0, 1)
80+
If readChars >= 1 Then
81+
Select Case convertMode
82+
Case TextConvertMode.Dos2Ux
83+
If readBuffer(0) = CR AndAlso oldFile.Peek() = 10 Then
84+
' Strip out CR chars if followed by LF
85+
Await oldFile.ReadAsync(readBuffer, 0, 1)
86+
End If
87+
Case TextConvertMode.Ux2Dos
88+
If readBuffer(0) = CR AndAlso oldFile.Peek() = 10 Then
89+
ReDim Preserve readBuffer(1)
90+
' This is a DOS line ending, keep it.
91+
Dim tempBuffer(1) As Char
92+
Await oldFile.ReadAsync(tempBuffer, 0, 1)
93+
readBuffer(1) = tempBuffer(0)
94+
ElseIf readBuffer(0) = LF Then
95+
ReDim readBuffer(1)
96+
' Add preceeding CR
97+
readBuffer(0) = CR
98+
readBuffer(1) = LF
99+
End If
100+
End Select
96101

97-
'Yield readBuffer
98-
convertedLines.Append(readBuffer)
99-
End If
100-
Loop
101-
End Using
102-
Catch ex As Exception
103-
Debug.Print("Error: " & ex.Message & Environment.NewLine & "Number: " & ex.HResult.ToString)
104-
Return ex.HResult.ToString
105-
End Try
102+
'Yield readBuffer
103+
convertedLines.Append(readBuffer)
104+
End If
105+
Loop
106+
End Using
106107

107108
Return convertedLines.ToString
108109
End Function
109-
110-
''' <summary>
111-
''' Attempt to detect the encoding of a file.
112-
''' </summary>
113-
''' <param name="filename">The file to get the encoding pattern from.</param>
114-
''' <returns>Encoding type, defaults to ASCII.</returns>
115-
Public Shared Function GetEncoding(theFile As FileStream) As Encoding
116-
Dim bom = New Byte(3) {}
117-
Try ' to read BOM
118-
theFile.Read(bom, 0, 4)
119-
Catch ex As Exception
120-
Debug.Print("Error: " & ex.Message & Environment.NewLine & "Number: " & ex.HResult.ToString)
121-
Return Nothing
122-
End Try
123-
124-
' Detect BOM type
125-
If bom(0) = &H2B AndAlso bom(1) = &H2F AndAlso bom(2) = &H76 Then
126-
Return Encoding.UTF7
127-
ElseIf bom(0) = &HEF AndAlso bom(1) = &HBB AndAlso bom(2) = &HBF Then
128-
Return Encoding.UTF8
129-
ElseIf bom(0) = &HFF AndAlso bom(1) = &HFE Then
130-
Return Encoding.Unicode
131-
ElseIf bom(0) = &HFE AndAlso bom(1) = &HFF Then
132-
Return Encoding.BigEndianUnicode
133-
ElseIf bom(0) = 0 AndAlso bom(1) = 0 AndAlso bom(2) = &HFE AndAlso bom(3) = &HFF Then
134-
Return Encoding.UTF32
135-
Else
136-
' Default to
137-
Return Encoding.ASCII
138-
End If
139-
End Function
140110
End Class

TextFileConvert/TextFileConvert.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<title>$title$</title>
77
<authors>$author$</authors>
88
<owners>$author$</owners>
9-
<license type="expression">MIT</license>
9+
<license type="expression">BSD-2-Clause-FreeBSD</license>
1010
<projectUrl>https://github.com/A9G-Data-Droid/TextFileConvert</projectUrl>
1111
<icon>ux2dos.ico</icon>
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
43
<RootNamespace>TextFileConvert</RootNamespace>
54
<TargetFramework>netstandard2.0</TargetFramework>
65
<Authors>Adam Kauffman</Authors>
7-
<Company>Cascade Engineering Technologies</Company>
86
<NeutralLanguage>en</NeutralLanguage>
97
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
108
<Description>This provides text file conversion capabilities like the common Unix utilities "ux2dos" and "dos2ux".</Description>
119
<Copyright>Copyright 2018 Adam Kauffman</Copyright>
12-
<PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
13-
<AssemblyVersion>0.1.0.1</AssemblyVersion>
14-
<Version>0.1.0</Version>
15-
<PackageReleaseNotes>This is the alpha build.</PackageReleaseNotes>
10+
<Deterministic>false</Deterministic>
11+
<AssemblyVersion>0.2.0.0</AssemblyVersion>
12+
<PackageReleaseNotes>This is the beta build.</PackageReleaseNotes>
1613
<PackageTags>ux2dos, dos2ux</PackageTags>
1714
<PackageProjectUrl>https://github.com/A9G-Data-Droid/TextFileConvert</PackageProjectUrl>
18-
<PackageIconUrl>https://github.com/A9G-Data-Droid/TextFileConvert/raw/master/TextFileConvert/ux2dos.ico</PackageIconUrl>
15+
<PackageIcon>ux2dos.png</PackageIcon>
1916
<RepositoryUrl>https://github.com/A9G-Data-Droid/TextFileConvert</RepositoryUrl>
2017
<RepositoryType>GitHub</RepositoryType>
18+
<PublishSingleFile>true</PublishSingleFile>
19+
<OptionStrict>On</OptionStrict>
20+
<OptionInfer>Off</OptionInfer>
2121
</PropertyGroup>
22-
22+
<ItemGroup>
23+
<None Include="ux2dos.png" Pack="true" PackagePath=""/>
24+
</ItemGroup>
2325
</Project>

TextFileConvert/common.vb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Imports System.IO
2+
Imports System.Text
3+
4+
5+
6+
Public Module common
7+
''' <summary>
8+
''' This is the command line help that is displayed when invalid parameters are given.
9+
''' </summary>
10+
''' <param name="programName"></param>
11+
Public Sub PrintUsage(programName As String)
12+
Console.WriteLine(
13+
"NAME
14+
{0} - Text file format converter
15+
16+
SYNOPSIS
17+
{0} oldfilename newfilename
18+
19+
DESCRIPTION
20+
{0} reads oldfilename and writes out newfilename, converting line endings.", programName)
21+
End Sub
22+
23+
''' <summary>
24+
''' Attempt to detect the encoding of a file.
25+
''' </summary>
26+
''' <param name="filename">The file to get the encoding pattern from.</param>
27+
''' <returns>Encoding type, defaults to ASCII.</returns>
28+
Public Async Function GetEncoding(theFile As FileStream) As Task(Of Encoding)
29+
Dim bom As Byte() = New Byte(3) {}
30+
Await theFile.ReadAsync(bom, 0, 4)
31+
32+
' Detect BOM type
33+
If bom(0) = &H2B AndAlso bom(1) = &H2F AndAlso bom(2) = &H76 Then
34+
Return Encoding.UTF7
35+
ElseIf bom(0) = &HEF AndAlso bom(1) = &HBB AndAlso bom(2) = &HBF Then
36+
Return Encoding.UTF8
37+
ElseIf bom(0) = &HFF AndAlso bom(1) = &HFE Then
38+
Return Encoding.Unicode
39+
ElseIf bom(0) = &HFE AndAlso bom(1) = &HFF Then
40+
Return Encoding.BigEndianUnicode
41+
ElseIf bom(0) = 0 AndAlso bom(1) = 0 AndAlso bom(2) = &HFE AndAlso bom(3) = &HFF Then
42+
Return Encoding.UTF32
43+
Else
44+
' Default to
45+
Return Encoding.ASCII
46+
End If
47+
End Function
48+
49+
''' <summary>
50+
''' Detect when the file in the given path is a symbolic link.
51+
''' </summary>
52+
''' <param name="path"></param>
53+
''' <returns>True if </returns>
54+
Public Function IsSymbolic(path As String) As Boolean
55+
Dim pathInfo As New FileInfo(path)
56+
Return pathInfo.Attributes.HasFlag(FileAttributes.ReparsePoint)
57+
End Function
58+
End Module

0 commit comments

Comments
 (0)