This repository was archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathServerCommandTest.cs
119 lines (101 loc) · 3.94 KB
/
ServerCommandTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis;
using Moq;
using Xunit;
namespace Microsoft.AspNetCore.Razor.Tools
{
public class ServerCommandTest
{
[Fact]
public void WritePidFile_WorksAsExpected()
{
// Arrange
var expectedProcessId = Process.GetCurrentProcess().Id;
var expectedRzcPath = typeof(ServerCommand).Assembly.Location;
var expectedFileName = $"rzc-{expectedProcessId}";
var directoryPath = Path.Combine(Path.GetTempPath(), "RazorTest", Guid.NewGuid().ToString());
var path = Path.Combine(directoryPath, expectedFileName);
var pipeName = Guid.NewGuid().ToString();
var server = GetServerCommand(pipeName);
// Act & Assert
try
{
using (var _ = server.WritePidFile(directoryPath))
{
Assert.True(File.Exists(path));
// Make sure another stream can be opened while the write stream is still open.
using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Write | FileShare.Delete))
using (var reader = new StreamReader(fileStream, Encoding.UTF8))
{
var lines = reader.ReadToEnd().Split(Environment.NewLine);
Assert.Equal(new[] { expectedProcessId.ToString(), "rzc", expectedRzcPath, pipeName }, lines);
}
}
// Make sure the file is deleted on dispose.
Assert.False(File.Exists(path));
}
finally
{
// Cleanup after the test.
if (Directory.Exists(directoryPath))
{
Directory.Delete(directoryPath, recursive: true);
}
}
}
[Fact]
public void GetPidFilePath_ReturnsCorrectDefaultPath()
{
// Arrange
var expectedPath = Path.Combine("homeDir", ".dotnet", "pids", "build");
var server = GetServerCommand();
// Act
var directoryPath = server.GetPidFilePath(getEnvironmentVariable: env =>
{
if (env == "DOTNET_BUILD_PIDFILE_DIRECTORY")
{
return null;
}
return "homeDir";
});
// Assert
Assert.Equal(expectedPath, directoryPath);
}
[Fact]
public void GetPidFilePath_UsesEnvironmentVariablePathIfSpecified()
{
// Arrange
var expectedPath = "/Some/directory/path/";
var server = GetServerCommand();
// Act
var directoryPath = server.GetPidFilePath(getEnvironmentVariable: env => expectedPath);
// Assert
Assert.Equal(expectedPath, directoryPath);
}
[Fact]
public void GetPidFilePath_NullEnvironmentVariableValue_ReturnsNull()
{
// Arrange
var server = GetServerCommand();
// Act
var directoryPath = server.GetPidFilePath(getEnvironmentVariable: env => null);
// Assert
Assert.Null(directoryPath);
}
private ServerCommand GetServerCommand(string pipeName = null)
{
var application = new Application(
CancellationToken.None,
Mock.Of<ExtensionAssemblyLoader>(),
Mock.Of<ExtensionDependencyChecker>(),
(path, properties) => MetadataReference.CreateFromFile(path, properties));
return new ServerCommand(application, pipeName);
}
}
}