Skip to content

Commit 736bc81

Browse files
committed
addressing comments by adding analyzer and addressing any issues
1 parent d623450 commit 736bc81

16 files changed

+260
-234
lines changed

Azure.Functions.Cli.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.Functions.Cli.Abstrac
2121
EndProject
2222
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreToolsHost", "src\CoreToolsHost\CoreToolsHost.csproj", "{0333D5B6-B628-4605-A51E-D0AEE4C3F1FC}"
2323
EndProject
24-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Func.TestFramework", "test\Cli\Func.TestFramework\Func.TestFramework.csproj", "{40C7D05D-93E9-40E9-AC37-2B36C5854477}"
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Azure.Functions.Cli.TestFramework", "test\Cli\Func.TestFramework\Azure.Functions.Cli.TestFramework.csproj", "{40C7D05D-93E9-40E9-AC37-2B36C5854477}"
2525
EndProject
2626
Global
2727
GlobalSection(SolutionConfigurationPlatforms) = preSolution

test/Cli/Func.TestFramework/Assertions/CommandResultAssertions.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
2-
// Licensed under the MIT License. See License.txt in the project root for license information.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
// Based off of: https://github.com/dotnet/sdk/blob/e793aa4709d28cd783712df40413448250e26fea/test/Microsoft.NET.TestFramework/Assertions/CommandResultAssertions.cs
5+
using Azure.Functions.Cli.Abstractions.Command;
56
using FluentAssertions;
67
using FluentAssertions.Execution;
78

8-
namespace Func.TestFramework.Assertions
9+
namespace Azure.Functions.Cli.TestFramework.Assertions
910
{
10-
public class CommandResultAssertions
11+
public class CommandResultAssertions(CommandResult commandResult)
1112
{
12-
private CommandResult _commandResult;
13-
14-
public CommandResultAssertions(CommandResult commandResult)
15-
{
16-
_commandResult = commandResult;
17-
}
13+
private readonly CommandResult _commandResult = commandResult;
1814

1915
public CommandResultAssertions ExitWith(int expectedExitCode)
2016
{
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
2-
// Licensed under the MIT License. See License.txt in the project root for license information.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
// Copied from: https://github.com/dotnet/sdk/blob/e793aa4709d28cd783712df40413448250e26fea/test/Microsoft.NET.TestFramework/Assertions/CommandResultExtensions.cs
5-
namespace Func.TestFramework.Assertions
5+
using Azure.Functions.Cli.Abstractions.Command;
6+
7+
namespace Azure.Functions.Cli.TestFramework.Assertions
68
{
79
public static class CommandResultExtensions
810
{
9-
public static CommandResultAssertions Should(this CommandResult commandResult) => new CommandResultAssertions(commandResult);
11+
public static CommandResultAssertions Should(this CommandResult commandResult) => new(commandResult);
1012
}
1113
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectCapability Remove="TestContainer" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Azure.Storage.Queues" />
14+
<PackageReference Include="xunit" />
15+
<PackageReference Include="FluentAssertions"/>
16+
<PackageReference Include="StyleCop.Analyzers" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="$(RepoSrcRoot)Cli\Abstractions\Azure.Functions.Cli.Abstractions.csproj" />
21+
</ItemGroup>
22+
23+
</Project>

test/Cli/Func.TestFramework/CommandInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
2-
// Licensed under the MIT License. See License.txt in the project root for license information.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
// Based off of: https://github.com/dotnet/sdk/blob/e793aa4709d28cd783712df40413448250e26fea/test/Microsoft.NET.TestFramework/Commands/SdkCommandSpec.cs
55
using System.Diagnostics;
6-
using Azure.Functions.Cli.Abstractions;
6+
using Azure.Functions.Cli.Abstractions.Command;
77

8-
namespace Func.TestFramework
8+
namespace Azure.Functions.Cli.TestFramework
99
{
1010
public class CommandInfo
1111
{

test/Cli/Func.TestFramework/Commands/FuncCommand.cs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
2-
// Licensed under the MIT License. See License.txt in the project root for license information.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
// Based off of: https://github.com/dotnet/sdk/blob/e793aa4709d28cd783712df40413448250e26fea/test/Microsoft.NET.TestFramework/Commands/TestCommand.cs
5-
6-
using Azure.Functions.Cli.Abstractions;
75
using System.Diagnostics;
6+
using Azure.Functions.Cli.Abstractions.Command;
87
using Xunit.Abstractions;
98

10-
namespace Func.TestFramework.Commands
9+
namespace Azure.Functions.Cli.TestFramework.Commands
1110
{
12-
public abstract class FuncCommand
11+
public abstract class FuncCommand(ITestOutputHelper log)
1312
{
14-
private Dictionary<string, string> _environment = new Dictionary<string, string>();
13+
private readonly Dictionary<string, string> _environment = [];
1514

16-
public ITestOutputHelper Log { get; }
15+
public ITestOutputHelper Log { get; } = log;
1716

18-
public string WorkingDirectory { get; set; }
17+
public string? WorkingDirectory { get; set; }
1918

20-
public List<string> Arguments { get; set; } = new List<string>();
19+
public List<string> Arguments { get; set; } = [];
2120

22-
public List<string> EnvironmentToRemove { get; } = new List<string>();
21+
public List<string> EnvironmentToRemove { get; } = [];
2322

24-
// These only work via Execute(), not when using GetProcessStartInfo()
23+
// These only work via Execute(), not when using GetProcessStartInfo()
2524
public Action<string>? CommandOutputHandler { get; set; }
2625

2726
public Func<Process, Task>? ProcessStartedHandler { get; set; }
2827

2928
public StreamWriter? FileWriter { get; private set; } = null;
3029

31-
public string LogFilePath { get; private set; }
32-
33-
protected FuncCommand(ITestOutputHelper log)
34-
{
35-
Log = log;
36-
}
30+
public string? LogFilePath { get; private set; }
3731

3832
protected abstract CommandInfo CreateCommand(IEnumerable<string> args);
3933

@@ -51,13 +45,13 @@ public FuncCommand WithWorkingDirectory(string workingDirectory)
5145

5246
private CommandInfo CreateCommandInfo(IEnumerable<string> args)
5347
{
54-
var commandInfo = CreateCommand(args);
55-
foreach (var kvp in _environment)
48+
CommandInfo commandInfo = CreateCommand(args);
49+
foreach (KeyValuePair<string, string> kvp in _environment)
5650
{
5751
commandInfo.Environment[kvp.Key] = kvp.Value;
5852
}
5953

60-
foreach (var envToRemove in EnvironmentToRemove)
54+
foreach (string envToRemove in EnvironmentToRemove)
6155
{
6256
commandInfo.EnvironmentToRemove.Add(envToRemove);
6357
}
@@ -67,33 +61,36 @@ private CommandInfo CreateCommandInfo(IEnumerable<string> args)
6761
commandInfo.WorkingDirectory = WorkingDirectory;
6862
}
6963

70-
if (Arguments.Any())
64+
if (Arguments.Count != 0)
7165
{
72-
commandInfo.Arguments = Arguments.Concat(commandInfo.Arguments).ToList();
66+
commandInfo.Arguments = [.. Arguments, .. commandInfo.Arguments];
7367
}
7468

7569
return commandInfo;
7670
}
7771

7872
public ProcessStartInfo GetProcessStartInfo(params string[] args)
7973
{
80-
var commandSpec = CreateCommandInfo(args);
74+
CommandInfo commandSpec = CreateCommandInfo(args);
8175
return commandSpec.ToProcessStartInfo();
8276
}
8377

8478
public virtual CommandResult Execute(IEnumerable<string> args)
8579
{
86-
var spec = CreateCommandInfo(args);
87-
var command = spec
80+
CommandInfo spec = CreateCommandInfo(args);
81+
ICommand command = spec
8882
.ToCommand()
8983
.CaptureStdOut()
9084
.CaptureStdErr();
9185

92-
var funcExeDirectory = Path.GetDirectoryName(spec.FileName);
86+
string? funcExeDirectory = Path.GetDirectoryName(spec.FileName);
9387

94-
Directory.SetCurrentDirectory(funcExeDirectory);
88+
if (!string.IsNullOrEmpty(funcExeDirectory))
89+
{
90+
Directory.SetCurrentDirectory(funcExeDirectory);
91+
}
9592

96-
var directoryToLogTo = Environment.GetEnvironmentVariable("DirectoryToLogTo");
93+
string? directoryToLogTo = Environment.GetEnvironmentVariable("DirectoryToLogTo");
9794
if (string.IsNullOrEmpty(directoryToLogTo))
9895
{
9996
directoryToLogTo = Directory.GetCurrentDirectory();
@@ -103,8 +100,9 @@ public virtual CommandResult Execute(IEnumerable<string> args)
103100
Directory.CreateDirectory(directoryToLogTo);
104101

105102
// Create a more unique filename to avoid conflicts
106-
string uniqueId = Guid.NewGuid().ToString("N").Substring(0, 8);
107-
LogFilePath = Path.Combine(directoryToLogTo,
103+
string uniqueId = Guid.NewGuid().ToString("N")[..8];
104+
LogFilePath = Path.Combine(
105+
directoryToLogTo,
108106
$"func_{spec.Arguments.First()}_{spec.TestName}_{DateTime.Now:yyyyMMdd_HHmmss}_{uniqueId}.log");
109107

110108
// Make sure we're only opening the file once
@@ -120,7 +118,7 @@ public virtual CommandResult Execute(IEnumerable<string> args)
120118
// Write initial information
121119
FileWriter.WriteLine($"=== Test started at {DateTime.Now} ===");
122120
FileWriter.WriteLine($"Test Name: {spec.TestName}");
123-
var display = $"func {string.Join(" ", spec.Arguments)}";
121+
string? display = $"func {string.Join(" ", spec.Arguments)}";
124122
FileWriter.WriteLine($"Command: {display}");
125123
FileWriter.WriteLine($"Working Directory: {spec.WorkingDirectory ?? "not specified"}");
126124
FileWriter.WriteLine("====================================");
@@ -170,7 +168,7 @@ public virtual CommandResult Execute(IEnumerable<string> args)
170168
Log.WriteLine($"Executing '{display}':");
171169
Log.WriteLine($"Output being captured to: {LogFilePath}");
172170

173-
var result = ((Command)command).Execute(ProcessStartedHandler, FileWriter);
171+
CommandResult result = ((Command)command).Execute(ProcessStartedHandler, FileWriter);
174172

175173
FileWriter.WriteLine("====================================");
176174
FileWriter.WriteLine($"Command exited with code: {result.ExitCode}");
@@ -205,7 +203,7 @@ public static void LogCommandResult(ITestOutputHelper log, CommandResult result)
205203

206204
if (!string.IsNullOrEmpty(result.StdErr))
207205
{
208-
log.WriteLine("");
206+
log.WriteLine(string.Empty);
209207
log.WriteLine("StdErr:");
210208
log.WriteLine(result.StdErr);
211209
}

test/Cli/Func.TestFramework/Commands/FuncInitCommand.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
2-
// Licensed under the MIT License. See License.txt in the project root for license information.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using Xunit.Abstractions;
55

6-
namespace Func.TestFramework.Commands
6+
namespace Azure.Functions.Cli.TestFramework.Commands
77
{
8-
public class FuncInitCommand : FuncCommand
8+
public class FuncInitCommand(string funcPath, string testName, ITestOutputHelper log) : FuncCommand(log)
99
{
10-
private readonly string CommandName = "init";
11-
private string _funcPath;
12-
private string _testName;
13-
14-
public FuncInitCommand(string funcPath, string testName, ITestOutputHelper log) : base(log)
15-
{
16-
_funcPath = funcPath;
17-
_testName = testName;
18-
}
10+
private readonly string _commandName = "init";
11+
private readonly string _funcPath = funcPath;
12+
private readonly string _testName = testName;
1913

2014
protected override CommandInfo CreateCommand(IEnumerable<string> args)
2115
{
22-
var arguments = new List<string> { CommandName }.Concat(args).ToList();
16+
var arguments = new List<string> { _commandName }.Concat(args).ToList();
17+
18+
if (WorkingDirectory is null)
19+
{
20+
throw new InvalidOperationException("Working Directory must be set");
21+
}
2322

2423
var commandInfo = new CommandInfo()
2524
{

test/Cli/Func.TestFramework/Commands/FuncNewCommand.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
2-
// Licensed under the MIT License. See License.txt in the project root for license information.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using Xunit.Abstractions;
55

6-
namespace Func.TestFramework.Commands
6+
namespace Azure.Functions.Cli.TestFramework.Commands
77
{
8-
public class FuncNewCommand : FuncCommand
8+
public class FuncNewCommand(string funcPath, string testName, ITestOutputHelper log) : FuncCommand(log)
99
{
10-
private readonly string CommandName = "new";
11-
private string _funcPath;
12-
private string _testName;
13-
14-
public FuncNewCommand(string funcPath, string testName, ITestOutputHelper log) : base(log)
15-
{
16-
_funcPath = funcPath;
17-
_testName = testName;
18-
}
10+
private readonly string _commandName = "new";
11+
private readonly string _funcPath = funcPath;
12+
private readonly string _testName = testName;
1913

2014
protected override CommandInfo CreateCommand(IEnumerable<string> args)
2115
{
22-
var arguments = new List<string> { CommandName }.Concat(args).ToList();
16+
var arguments = new List<string> { _commandName }.Concat(args).ToList();
17+
18+
if (WorkingDirectory is null)
19+
{
20+
throw new InvalidOperationException("Working Directory must be set");
21+
}
2322

2423
var commandInfo = new CommandInfo()
2524
{

test/Cli/Func.TestFramework/Commands/FuncSettingsCommand.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
2-
// Licensed under the MIT License. See License.txt in the project root for license information.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using Xunit.Abstractions;
55

6-
namespace Func.TestFramework.Commands
6+
namespace Azure.Functions.Cli.TestFramework.Commands
77
{
8-
public class FuncSettingsCommand : FuncCommand
8+
public class FuncSettingsCommand(string funcPath, string testName, ITestOutputHelper log) : FuncCommand(log)
99
{
10-
private readonly string CommandName = "settings";
11-
private string _funcPath;
12-
private string _testName;
13-
14-
public FuncSettingsCommand(string funcPath, string testName, ITestOutputHelper log) : base(log)
15-
{
16-
_funcPath = funcPath;
17-
_testName = testName;
18-
}
10+
private readonly string _commandName = "settings";
11+
private readonly string _funcPath = funcPath;
12+
private readonly string _testName = testName;
1913

2014
protected override CommandInfo CreateCommand(IEnumerable<string> args)
2115
{
22-
var arguments = new List<string> { CommandName }.Concat(args).ToList();
16+
var arguments = new List<string> { _commandName }.Concat(args).ToList();
17+
18+
if (WorkingDirectory is null)
19+
{
20+
throw new InvalidOperationException("Working Directory must be set");
21+
}
2322

2423
var commandInfo = new CommandInfo()
2524
{

test/Cli/Func.TestFramework/Commands/FuncStartCommand.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
2-
// Licensed under the MIT License. See License.txt in the project root for license information.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using Xunit.Abstractions;
55

6-
namespace Func.TestFramework.Commands
6+
namespace Azure.Functions.Cli.TestFramework.Commands
77
{
8-
public class FuncStartCommand : FuncCommand
8+
public class FuncStartCommand(string funcPath, string testName, ITestOutputHelper log) : FuncCommand(log)
99
{
10-
private readonly string CommandName = "start";
11-
private string _funcPath;
12-
private string _testName;
13-
14-
public FuncStartCommand(string funcPath, string testName, ITestOutputHelper log) : base(log)
15-
{
16-
_funcPath = funcPath;
17-
_testName = testName;
18-
}
10+
private readonly string _commandName = "start";
11+
private readonly string _funcPath = funcPath;
12+
private readonly string _testName = testName;
1913

2014
protected override CommandInfo CreateCommand(IEnumerable<string> args)
2115
{
22-
var arguments = new List<string> { CommandName }.Concat(args).ToList();
16+
var arguments = new List<string> { _commandName }.Concat(args).ToList();
17+
18+
if (WorkingDirectory is null)
19+
{
20+
throw new InvalidOperationException("Working Directory must be set");
21+
}
2322

2423
var commandInfo = new CommandInfo()
2524
{

0 commit comments

Comments
 (0)