Merge pull request #3914 from brthor/brthor/readonlytest
Add Test for removing readonly flag when copying readonly library assets
This commit is contained in:
commit
30557a179a
2 changed files with 70 additions and 2 deletions
|
@ -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()
|
|
@ -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<LibraryAsset> 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<LibraryAsset> 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue