diff --git a/ClosedXML_Tests/Excel/Columns/ColumnTests.cs b/ClosedXML_Tests/Excel/Columns/ColumnTests.cs index aa28593..4297a99 100644 --- a/ClosedXML_Tests/Excel/Columns/ColumnTests.cs +++ b/ClosedXML_Tests/Excel/Columns/ColumnTests.cs @@ -107,7 +107,7 @@ IXLColumn column3 = ws.Column(3); IXLColumn columnIns = ws.Column(2).InsertColumnsBefore(1).First(); - string outputPath = Path.Combine(TestHelper.TestsOutputDirectory, @"ForTesting\Sandbox.xlsx"); + string outputPath = Path.Combine(TestHelper.TestsOutputDirectory, "ForTesting", "Sandbox.xlsx"); wb.SaveAs(outputPath, true); Assert.AreEqual(XLColor.Red, ws.Column(1).Cell(1).Style.Fill.BackgroundColor); diff --git a/ClosedXML_Tests/Excel/Misc/CopyContentsTests.cs b/ClosedXML_Tests/Excel/Misc/CopyContentsTests.cs index ce055cc..c9c77f6 100644 --- a/ClosedXML_Tests/Excel/Misc/CopyContentsTests.cs +++ b/ClosedXML_Tests/Excel/Misc/CopyContentsTests.cs @@ -113,7 +113,7 @@ copyRowSheet.Cell("G2").Value = "must be removed after copy"; originalRow.CopyTo(destinationRow); } - TestHelper.SaveWorkbook(workbook, @"Misc\CopyRowContents.xlsx"); + TestHelper.SaveWorkbook(workbook, "Misc", "CopyRowContents.xlsx"); } } } \ No newline at end of file diff --git a/ClosedXML_Tests/ExcelDocsComparerTests.cs b/ClosedXML_Tests/ExcelDocsComparerTests.cs index 3397f25..ed16735 100644 --- a/ClosedXML_Tests/ExcelDocsComparerTests.cs +++ b/ClosedXML_Tests/ExcelDocsComparerTests.cs @@ -17,7 +17,7 @@ new BasicTable().Create(left); new BasicTable().Create(right); string message; - Assert.IsTrue(ExcelDocsComparer.Compare(left, right, out message)); + Assert.IsTrue(ExcelDocsComparer.Compare(left, right, TestHelper.IsRunningOnUnix, out message)); } finally { @@ -43,7 +43,7 @@ new HelloWorld().Create(right); string message; - Assert.IsFalse(ExcelDocsComparer.Compare(left, right, out message)); + Assert.IsFalse(ExcelDocsComparer.Compare(left, right, TestHelper.IsRunningOnUnix, out message)); } finally { diff --git a/ClosedXML_Tests/TestHelper.cs b/ClosedXML_Tests/TestHelper.cs index 36b5933..477facf 100644 --- a/ClosedXML_Tests/TestHelper.cs +++ b/ClosedXML_Tests/TestHelper.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.IO; using System.Threading; using ClosedXML.Excel; @@ -19,7 +21,8 @@ //Note: Run example tests parameters public static string TestsOutputDirectory { - get { + get + { return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); } @@ -32,9 +35,22 @@ private static readonly ResourceFileExtractor _extractor = new ResourceFileExtractor(null, ".Resource.Examples."); - public static void SaveWorkbook(XLWorkbook workbook, string fileName) + public static void SaveWorkbook(XLWorkbook workbook, params string[] fileNameParts) { - workbook.SaveAs(Path.Combine(TestsOutputDirectory, fileName), true); + workbook.SaveAs(Path.Combine(new string[] { TestsOutputDirectory }.Concat(fileNameParts).ToArray()), true); + } + + // Because different fonts are installed on Unix, + // the columns widths after AdjustToContents() will + // cause the tests to fail. + // Therefore we ignore the width attribute when running on Unix + public static bool IsRunningOnUnix + { + get + { + int p = (int)Environment.OSVersion.Platform; + return ((p == 4) || (p == 6) || (p == 128)); + } } public static void RunTestExample(string filePartName) @@ -44,7 +60,8 @@ Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); var example = new T(); - string filePath1 = Path.Combine(TestsExampleOutputDirectory, filePartName); + string[] pathParts = filePartName.Split(new char[] {'\\'}); + string filePath1 = Path.Combine(new List() { TestsExampleOutputDirectory }.Concat(pathParts).ToArray()); var extension = Path.GetExtension(filePath1); var directory = Path.GetDirectoryName(filePath1); @@ -73,7 +90,7 @@ using (var streamActual = File.OpenRead(filePath2)) { string message; - success = ExcelDocsComparer.Compare(streamActual, streamExpected, out message); + success = ExcelDocsComparer.Compare(streamActual, streamExpected, TestHelper.IsRunningOnUnix, out message); var formattedMessage = String.Format( "Actual file '{0}' is different than the expected file '{1}'. The difference is: '{2}'", diff --git a/ClosedXML_Tests/Utils/ExcelDocsComparer.cs b/ClosedXML_Tests/Utils/ExcelDocsComparer.cs index 25dc07f..0bbeafd 100644 --- a/ClosedXML_Tests/Utils/ExcelDocsComparer.cs +++ b/ClosedXML_Tests/Utils/ExcelDocsComparer.cs @@ -6,24 +6,24 @@ { internal static class ExcelDocsComparer { - public static bool Compare(string left, string right, out string message) + public static bool Compare(string left, string right, bool stripColumnWidths, out string message) { using (FileStream leftStream = File.OpenRead(left)) { using (FileStream rightStream = File.OpenRead(right)) { - return Compare(leftStream, rightStream, out message); + return Compare(leftStream, rightStream, stripColumnWidths, out message); } } } - public static bool Compare(Stream left, Stream right, out string message) + public static bool Compare(Stream left, Stream right, bool stripColumnWidths, out string message) { using (Package leftPackage = Package.Open(left)) { using (Package rightPackage = Package.Open(right)) { - return PackageHelper.Compare(leftPackage, rightPackage, false, ExcludeMethod, out message); + return PackageHelper.Compare(leftPackage, rightPackage, false, ExcludeMethod, stripColumnWidths, out message); } } } diff --git a/ClosedXML_Tests/Utils/PackageHelper.cs b/ClosedXML_Tests/Utils/PackageHelper.cs index ec92716..33ba3e4 100644 --- a/ClosedXML_Tests/Utils/PackageHelper.cs +++ b/ClosedXML_Tests/Utils/PackageHelper.cs @@ -273,9 +273,9 @@ /// /// /// - public static bool Compare(Package left, Package right, bool compareToFirstDifference, out string message) + public static bool Compare(Package left, Package right, bool compareToFirstDifference, bool stripColumnWidths, out string message) { - return Compare(left, right, compareToFirstDifference, null, out message); + return Compare(left, right, compareToFirstDifference, null, stripColumnWidths, out message); } /// @@ -288,7 +288,7 @@ /// /// public static bool Compare(Package left, Package right, bool compareToFirstDifference, - Func excludeMethod, out string message) + Func excludeMethod, bool stripColumnWidths, out string message) { #region Check @@ -344,10 +344,16 @@ { continue; } - using (Stream oneStream = left.GetPart(pair.Uri).GetStream(FileMode.Open, FileAccess.Read)) - using (Stream otherStream = right.GetPart(pair.Uri).GetStream(FileMode.Open, FileAccess.Read)) + var leftPart = left.GetPart(pair.Uri); + var rightPart = right.GetPart(pair.Uri); + using (Stream oneStream = leftPart.GetStream(FileMode.Open, FileAccess.Read)) + using (Stream otherStream = rightPart.GetStream(FileMode.Open, FileAccess.Read)) { - if (!StreamHelper.Compare(oneStream, otherStream)) + bool stripColumnWidthsFromSheet = stripColumnWidths && + leftPart.ContentType == @"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" && + rightPart.ContentType == @"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"; + + if (!StreamHelper.Compare(oneStream, otherStream, stripColumnWidthsFromSheet)) { pair.Status = CompareStatus.NonEqual; if (compareToFirstDifference) diff --git a/ClosedXML_Tests/Utils/StreamHelper.cs b/ClosedXML_Tests/Utils/StreamHelper.cs index 30b8d08..05a057e 100644 --- a/ClosedXML_Tests/Utils/StreamHelper.cs +++ b/ClosedXML_Tests/Utils/StreamHelper.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Text.RegularExpressions; namespace ClosedXML_Tests { @@ -19,7 +22,7 @@ var bytes = new byte[iLength]; for (int i = 0; i < iLength; i++) { - bytes[i] = (byte) pStream.ReadByte(); + bytes[i] = (byte)pStream.ReadByte(); } pStream.Close(); return bytes; @@ -98,7 +101,7 @@ long rest = length; while (rest > 0) { - int len1 = streamIn.Read(buf, 0, rest >= 512 ? 512 : (int) rest); + int len1 = streamIn.Read(buf, 0, rest >= 512 ? 512 : (int)rest); streamToWrite.Write(buf, 0, len1); rest -= len1; } @@ -109,8 +112,9 @@ /// /// /// + /// /// /// - public static bool Compare(Stream one, Stream other) + public static bool Compare(Stream one, Stream other, bool stripColumnWidths) { #region Check @@ -133,9 +137,35 @@ #endregion - var stringOne = new StreamReader(one).ReadToEnd(); - var stringOther = new StreamReader(other).ReadToEnd(); + var stringOne = new StreamReader(one).ReadToEnd().StripColumnWidths(stripColumnWidths); + var stringOther = new StreamReader(other).ReadToEnd().StripColumnWidths(stripColumnWidths); return stringOne == stringOther; } + + private static Regex columnRegex = new Regex("", RegexOptions.Compiled); + private static Regex widthRegex = new Regex("width=\"\\d+(\\.\\d+)?\"\\s+", RegexOptions.Compiled); + + private static string StripColumnWidths(this string s, bool stripIt) + { + if (!stripIt) + return s; + else + { + var replacements = new Dictionary(); + + foreach (var m in columnRegex.Matches(s).OfType()) + { + var original = m.Groups[0].Value; + var replacement = widthRegex.Replace(original, ""); + replacements.Add(original, replacement); + } + + foreach (var r in replacements) + { + s = s.Replace(r.Key, r.Value); + } + return s; + } + } } } \ No newline at end of file