diff --git a/test/Microsoft.DotNet.Compiler.Common.Tests/Tests.cs b/test/Microsoft.DotNet.Compiler.Common.Tests/GivenCommonCompilerOptions.cs similarity index 91% rename from test/Microsoft.DotNet.Compiler.Common.Tests/Tests.cs rename to test/Microsoft.DotNet.Compiler.Common.Tests/GivenCommonCompilerOptions.cs index b678464b1..6757af610 100644 --- a/test/Microsoft.DotNet.Compiler.Common.Tests/Tests.cs +++ b/test/Microsoft.DotNet.Compiler.Common.Tests/GivenCommonCompilerOptions.cs @@ -5,9 +5,9 @@ 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 + 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 new file mode 100644 index 000000000..b8421c27b --- /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(); + IsFileReadonly(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(); + IsFileReadonly(copiedFile).Should().BeFalse(); + } + + private void MakeFileReadonly(string file) + { + File.SetAttributes(file, File.GetAttributes(file) | FileAttributes.ReadOnly); + } + + private bool IsFileReadonly(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); + } + } +}