Skip to content

Commit

Permalink
Allow source and transform files to be read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
icnocop committed Mar 22, 2018
1 parent 03e1ea9 commit d8cb6d5
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/Microsoft.VisualStudio.Jdt.Tests/JsonTransformationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,31 @@ public void ThrowAndLogException()
}
}

/// <summary>
/// Tests that a transformation succeeds even if the source and transform files are read-only.
/// </summary>
[Fact]
public void ReadOnly()
{
const string TransformSourceString = @"{
'@jdt.rename' : {
'A' : 'Astar'
}
}";

// create temporary files to use for the source and transform
using (ReadOnlyTempFile tempSourceFilePath = new ReadOnlyTempFile(SimpleSourceString))
using (ReadOnlyTempFile tempTransformFilePath = new ReadOnlyTempFile(TransformSourceString))
{
// apply transform
JsonTransformation transformation = new JsonTransformation(tempSourceFilePath.FilePath, this.logger);
using (Stream result = transformation.Apply(tempTransformFilePath.FilePath))
{
// succeess
}
}
}

private static void LogHasSingleEntry(List<JsonTransformationTestLogger.TestLogEntry> log, string fileName, int lineNumber, int linePosition, bool fromException)
{
Assert.Single(log);
Expand Down
68 changes: 68 additions & 0 deletions src/Microsoft.VisualStudio.Jdt.Tests/ReadOnlyTempFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.Jdt.Tests
{
using System;
using System.IO;

/// <summary>
/// Read-Only Temp File.
/// </summary>
/// <seealso cref="System.IDisposable" />
internal class ReadOnlyTempFile : IDisposable
{
/// <summary>
/// Initializes a new instance of the <see cref="ReadOnlyTempFile"/> class.
/// </summary>
/// <param name="contents">The contents.</param>
public ReadOnlyTempFile(string contents)
{
// create temp file
this.FilePath = Path.GetTempFileName();

// write contents
File.WriteAllText(this.FilePath, contents);

// set the file as read-only
File.SetAttributes(this.FilePath, FileAttributes.ReadOnly);
}

/// <summary>
/// Gets the file path.
/// </summary>
/// <value>The file path.</value>
public string FilePath { get; private set; }

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
// delete file
if (this.FilePath == null)
{
// nothing to delete
return;
}

if (!File.Exists(this.FilePath))
{
// nothing to delete
return;
}

// remove read-only attribute if it exists
FileAttributes attributes = File.GetAttributes(this.FilePath);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
// read-only attribute exists
// remove read-only attribute
File.SetAttributes(this.FilePath, attributes ^ FileAttributes.ReadOnly);
}

// delete the file
File.Delete(this.FilePath);
}
}
}
4 changes: 2 additions & 2 deletions src/Microsoft.VisualStudio.Jdt/JsonTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public JsonTransformation(string transformFile, IJsonTransformationLogger logger

this.logger = new JsonTransformationContextLogger(transformFile, logger);

using (FileStream transformStream = File.Open(transformFile, FileMode.Open))
using (FileStream transformStream = File.Open(transformFile, FileMode.Open, FileAccess.Read))
{
this.SetTransform(transformStream);
}
Expand Down Expand Up @@ -86,7 +86,7 @@ public Stream Apply(string sourceFile)
}

// Open the file as streams and apply the transforms
using (Stream sourceStream = File.Open(sourceFile, FileMode.Open))
using (Stream sourceStream = File.Open(sourceFile, FileMode.Open, FileAccess.Read))
{
return this.ApplyWithSourceName(sourceStream, sourceFile);
}
Expand Down

0 comments on commit d8cb6d5

Please sign in to comment.