From e1c5c7a43d8addb3c40a64716978cae385d185f3 Mon Sep 17 00:00:00 2001 From: Bryan Thornbury Date: Thu, 21 Jul 2016 19:36:45 -0700 Subject: [PATCH 1/3] Add Test for removing readonly flag when copying readonly library assets --- .../GivenThatICopyLibraryAssets.cs | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 test/Microsoft.DotNet.Compiler.Common.Tests/GivenThatICopyLibraryAssets.cs diff --git a/test/Microsoft.DotNet.Compiler.Common.Tests/GivenThatICopyLibraryAssets.cs b/test/Microsoft.DotNet.Compiler.Common.Tests/GivenThatICopyLibraryAssets.cs new file mode 100644 index 000000000..7a3661d36 --- /dev/null +++ b/test/Microsoft.DotNet.Compiler.Common.Tests/GivenThatICopyLibraryAssets.cs @@ -0,0 +1,68 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using FluentAssertions; +using Xunit; +using Microsoft.DotNet.Cli.Compiler.Common; +using Microsoft.DotNet.ProjectModel.Compilation; +using System.IO; +using System; +using System.Linq; +using System.Collections.Generic; + +namespace Microsoft.DotNet.Cli.Compiler.Common.Tests +{ + public class GivenThatICopyLibraryAssets + { + [Fact] + public void LibraryAsset_CopyTo_Clears_Readonly() + { + var libraryAsset = GetMockLibraryAsset(nameof(LibraryAsset_CopyTo_Clears_Readonly)); + MakeFileReadonly(libraryAsset.ResolvedPath); + + IEnumerable assets = new LibraryAsset[] { libraryAsset }; + + var outputDirectory = Path.Combine(AppContext.BaseDirectory,$"{nameof(LibraryAsset_CopyTo_Clears_Readonly)}_out"); + assets.CopyTo(outputDirectory); + + var copiedFile = Directory.EnumerateFiles(outputDirectory, Path.GetFileName(libraryAsset.RelativePath)).First(); + FileIsReadonly(copiedFile).Should().BeFalse(); + } + + [Fact] + public void LibraryAsset_StructuredCopyTo_Clears_Readonly() + { + var libraryAsset = GetMockLibraryAsset(nameof(LibraryAsset_StructuredCopyTo_Clears_Readonly)); + MakeFileReadonly(libraryAsset.ResolvedPath); + + IEnumerable assets = new LibraryAsset[] { libraryAsset }; + + var intermediateDirectory = Path.Combine(AppContext.BaseDirectory,$"{nameof(LibraryAsset_StructuredCopyTo_Clears_Readonly)}_obj"); + var outputDirectory = Path.Combine(AppContext.BaseDirectory,$"{nameof(LibraryAsset_StructuredCopyTo_Clears_Readonly)}_out"); + assets.StructuredCopyTo(outputDirectory, intermediateDirectory); + + var copiedFile = Directory.EnumerateFiles(outputDirectory, Path.GetFileName(libraryAsset.RelativePath)).First(); + FileIsReadonly(copiedFile).Should().BeFalse(); + } + + private void MakeFileReadonly(string file) + { + File.SetAttributes(file, File.GetAttributes(file) | FileAttributes.ReadOnly); + } + + private bool FileIsReadonly(string file) + { + return (File.GetAttributes(file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly; + } + + private LibraryAsset GetMockLibraryAsset(string mockedLibraryAssetName) + { + var mockedLibraryAssetFileName = $"{mockedLibraryAssetName}.dll"; + + var fakeFile = Path.Combine(AppContext.BaseDirectory, mockedLibraryAssetFileName); + File.WriteAllText(fakeFile, mockedLibraryAssetName); + + return new LibraryAsset(mockedLibraryAssetName, mockedLibraryAssetFileName, fakeFile); + } + } +} From f829d8cb93d524ee044f4658d2ea52c1349fc2e3 Mon Sep 17 00:00:00 2001 From: Bryan Thornbury Date: Thu, 21 Jul 2016 21:12:50 -0700 Subject: [PATCH 2/3] change namespace --- test/Microsoft.DotNet.Compiler.Common.Tests/Tests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Microsoft.DotNet.Compiler.Common.Tests/Tests.cs b/test/Microsoft.DotNet.Compiler.Common.Tests/Tests.cs index b678464b1..20a57d77b 100644 --- a/test/Microsoft.DotNet.Compiler.Common.Tests/Tests.cs +++ b/test/Microsoft.DotNet.Compiler.Common.Tests/Tests.cs @@ -5,7 +5,7 @@ using Microsoft.DotNet.ProjectModel; using Microsoft.DotNet.Tools.Test.Utilities; using Xunit; -namespace Microsoft.DotNet.Cli.Compiler.Common +namespace Microsoft.DotNet.Cli.Compiler.Common.Tests { public class Tests : TestBase { From 5cb8be5143e2d4e2f9e994cdf9193755284ecf52 Mon Sep 17 00:00:00 2001 From: Bryan Thornbury Date: Thu, 21 Jul 2016 22:09:07 -0700 Subject: [PATCH 3/3] PR Feedback --- .../{Tests.cs => GivenCommonCompilerOptions.cs} | 2 +- .../GivenThatICopyLibraryAssets.cs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) rename test/Microsoft.DotNet.Compiler.Common.Tests/{Tests.cs => GivenCommonCompilerOptions.cs} (95%) diff --git a/test/Microsoft.DotNet.Compiler.Common.Tests/Tests.cs b/test/Microsoft.DotNet.Compiler.Common.Tests/GivenCommonCompilerOptions.cs similarity index 95% rename from test/Microsoft.DotNet.Compiler.Common.Tests/Tests.cs rename to test/Microsoft.DotNet.Compiler.Common.Tests/GivenCommonCompilerOptions.cs index 20a57d77b..6757af610 100644 --- a/test/Microsoft.DotNet.Compiler.Common.Tests/Tests.cs +++ b/test/Microsoft.DotNet.Compiler.Common.Tests/GivenCommonCompilerOptions.cs @@ -7,7 +7,7 @@ using Xunit; namespace Microsoft.DotNet.Cli.Compiler.Common.Tests { - public class Tests : TestBase + public class GivenCommonCompilerOptions : TestBase { [Fact] public void SimpleSerialize() diff --git a/test/Microsoft.DotNet.Compiler.Common.Tests/GivenThatICopyLibraryAssets.cs b/test/Microsoft.DotNet.Compiler.Common.Tests/GivenThatICopyLibraryAssets.cs index 7a3661d36..b8421c27b 100644 --- a/test/Microsoft.DotNet.Compiler.Common.Tests/GivenThatICopyLibraryAssets.cs +++ b/test/Microsoft.DotNet.Compiler.Common.Tests/GivenThatICopyLibraryAssets.cs @@ -26,7 +26,7 @@ namespace Microsoft.DotNet.Cli.Compiler.Common.Tests assets.CopyTo(outputDirectory); var copiedFile = Directory.EnumerateFiles(outputDirectory, Path.GetFileName(libraryAsset.RelativePath)).First(); - FileIsReadonly(copiedFile).Should().BeFalse(); + IsFileReadonly(copiedFile).Should().BeFalse(); } [Fact] @@ -42,7 +42,7 @@ namespace Microsoft.DotNet.Cli.Compiler.Common.Tests assets.StructuredCopyTo(outputDirectory, intermediateDirectory); var copiedFile = Directory.EnumerateFiles(outputDirectory, Path.GetFileName(libraryAsset.RelativePath)).First(); - FileIsReadonly(copiedFile).Should().BeFalse(); + IsFileReadonly(copiedFile).Should().BeFalse(); } private void MakeFileReadonly(string file) @@ -50,7 +50,7 @@ namespace Microsoft.DotNet.Cli.Compiler.Common.Tests File.SetAttributes(file, File.GetAttributes(file) | FileAttributes.ReadOnly); } - private bool FileIsReadonly(string file) + private bool IsFileReadonly(string file) { return (File.GetAttributes(file) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly; }