Skip to content
This repository was archived by the owner on Oct 31, 2025. It is now read-only.

Commit 2da622b

Browse files
committed
Unit Tests
1 parent 89400c3 commit 2da622b

File tree

13 files changed

+237
-68
lines changed

13 files changed

+237
-68
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace LocalStack.AwsLocal.AmbientContexts
4+
{
5+
public abstract class ConsoleContext
6+
{
7+
private static ConsoleContext _current = DefaultConsoleContext.Instance;
8+
9+
public static ConsoleContext Current
10+
{
11+
get => _current;
12+
13+
set => _current = value ?? throw new ArgumentNullException(nameof(value));
14+
}
15+
16+
public abstract void WriteLine(string text);
17+
18+
public static void ResetToDefault()
19+
{
20+
_current = DefaultConsoleContext.Instance;
21+
}
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace LocalStack.AwsLocal.AmbientContexts
4+
{
5+
public class DefaultConsoleContext : ConsoleContext
6+
{
7+
private static readonly Lazy<DefaultConsoleContext> LazyInstance = new Lazy<DefaultConsoleContext>(() => new DefaultConsoleContext());
8+
9+
public override void WriteLine(string text)
10+
{
11+
Console.WriteLine(text);
12+
}
13+
14+
public static DefaultConsoleContext Instance => LazyInstance.Value;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace LocalStack.AwsLocal.AmbientContexts
4+
{
5+
public class DefaultEnvironmentContext : EnvironmentContext
6+
{
7+
private static readonly Lazy<DefaultEnvironmentContext> LazyInstance = new Lazy<DefaultEnvironmentContext>(() => new DefaultEnvironmentContext());
8+
9+
public override void Exit(int value)
10+
{
11+
Environment.Exit(value);
12+
}
13+
14+
public static DefaultEnvironmentContext Instance => LazyInstance.Value;
15+
}
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace LocalStack.AwsLocal.AmbientContexts
4+
{
5+
public abstract class EnvironmentContext
6+
{
7+
private static EnvironmentContext _current = DefaultEnvironmentContext.Instance;
8+
9+
public static EnvironmentContext Current
10+
{
11+
get => _current;
12+
13+
set => _current = value ?? throw new ArgumentNullException(nameof(value));
14+
}
15+
16+
public abstract void Exit(int value);
17+
18+
public static void ResetToDefault()
19+
{
20+
_current = DefaultEnvironmentContext.Instance;
21+
}
22+
}
23+
}

src/LocalStack.AwsLocal/CommandDispatcher.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Reflection;
5+
using LocalStack.AwsLocal.AmbientContexts;
56
using LocalStack.AwsLocal.Contracts;
6-
using LocalStack.AwsLocal.EnvironmentContext;
77
using LocalStack.AwsLocal.Extensions;
88
using LocalStack.Client.Contracts;
99
using LocalStack.Client.Models;
@@ -16,18 +16,16 @@ public class CommandDispatcher
1616

1717
private readonly IProcessHelper _processHelper;
1818
private readonly IConfig _config;
19-
private readonly TextWriter _textWriter;
2019
private readonly string[] _args;
2120

2221
private CommandDispatcher()
2322
{
2423
}
2524

26-
public CommandDispatcher(IProcessHelper processHelper, IConfig config, TextWriter textWriter, string[] args)
25+
public CommandDispatcher(IProcessHelper processHelper, IConfig config, string[] args)
2726
{
2827
_processHelper = processHelper;
2928
_config = config;
30-
_textWriter = textWriter;
3129
_args = args;
3230
}
3331

@@ -36,26 +34,26 @@ public void Run()
3634
if (_args.Length == 0 || (_args[0] == "-h"))
3735
{
3836
string usageInfo = GetUsageInfo();
39-
_textWriter.WriteLine(usageInfo);
40-
EnvironmentControl.Current.Exit(0);
37+
ConsoleContext.Current.WriteLine(usageInfo);
38+
EnvironmentContext.Current.Exit(0);
4139
return;
4240
}
4341

4442
string serviceName = _args.ExtractServiceName();
4543

4644
if (string.IsNullOrEmpty(serviceName))
4745
{
48-
_textWriter.WriteLine("ERROR: Invalid argument, please enter a valid aws cli command");
49-
EnvironmentControl.Current.Exit(1);
46+
ConsoleContext.Current.WriteLine("ERROR: Invalid argument, please enter a valid aws cli command");
47+
EnvironmentContext.Current.Exit(1);
5048
return;
5149
}
5250

5351
AwsServiceEndpoint awsServiceEndpoint = _config.GetServiceEndpoint(serviceName);
5452

5553
if (awsServiceEndpoint == null)
5654
{
57-
_textWriter.WriteLine($"ERROR: Unable to find LocalStack endpoint for service {serviceName}");
58-
EnvironmentControl.Current.Exit(1);
55+
ConsoleContext.Current.WriteLine($"ERROR: Unable to find LocalStack endpoint for service {serviceName}");
56+
EnvironmentContext.Current.Exit(1);
5957
return;
6058
}
6159

src/LocalStack.AwsLocal/EnvironmentContext/DefaultEnvironmentControl.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/LocalStack.AwsLocal/EnvironmentContext/EnvironmentControl.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/LocalStack.AwsLocal/LocalStack.AwsLocal.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<Folder Include="Depedencies\Contracts\" />
16-
<Folder Include="Depedencies\Models\" />
17-
<Folder Include="Depedencies\Enums\" />
18-
1915
<Compile Include="..\..\depedencies\localstack-dotnet-client\src\LocalStack.Client\Config.cs" Link="Depedencies\Config.cs" />
2016
<Compile Include="..\..\depedencies\localstack-dotnet-client\src\LocalStack.Client\Contracts\IConfig.cs" Link="Depedencies\Contracts\IConfig.cs" />
2117
<Compile Include="..\..\depedencies\localstack-dotnet-client\src\LocalStack.Client\Enums\AwsServiceEndpointMetadata.cs" Link="Depedencies\Enums\AwsServiceEndpointMetadata.cs" />

src/LocalStack.AwsLocal/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ private static void Main(string[] args)
1111
{
1212
var processHelper = new ProcessHelper();
1313
var config = new Config(LocalStackHost);
14-
var textWriter = Console.Out;
1514

16-
var commandDispatcher = new CommandDispatcher(processHelper, config, textWriter, args);
15+
var commandDispatcher = new CommandDispatcher(processHelper, config, args);
1716

1817
commandDispatcher.Run();
1918
}
Lines changed: 115 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,129 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
2+
using LocalStack.AwsLocal.AmbientContexts;
3+
using LocalStack.AwsLocal.Extensions;
44
using LocalStack.AwsLocal.Tests.Mocks;
5-
using Microsoft.VisualStudio.TestPlatform.TestHost;
5+
using LocalStack.Client.Enums;
6+
using LocalStack.Client.Models;
7+
using Moq;
68
using Xunit;
79

810
namespace LocalStack.AwsLocal.Tests
911
{
1012
public class CommandDispatcherTests
1113
{
14+
private readonly EnvironmentContextMock _environmentContextMock;
15+
private readonly ConsoleContextMock _consoleContextMock;
16+
17+
public CommandDispatcherTests()
18+
{
19+
_environmentContextMock = new EnvironmentContextMock();
20+
_consoleContextMock = new ConsoleContextMock();
21+
22+
EnvironmentContext.Current = _environmentContextMock;
23+
ConsoleContext.Current = _consoleContextMock;
24+
}
25+
26+
[Fact]
27+
public void Run_Should_Write_Help_Info_And_Exit_With_Zero_If_Argument_Count_Zero()
28+
{
29+
CommandDispatcherMock commandDispatcher = CommandDispatcherMock.Create(new string[0]);
30+
31+
commandDispatcher.Run();
32+
33+
Assert.Equal(0, _environmentContextMock.ExitValue);
34+
Assert.NotNull(_consoleContextMock.Text);
35+
Assert.NotEmpty(_consoleContextMock.Text);
36+
}
37+
38+
[Fact]
39+
public void Run_Should_Write_Help_Info_And_Exit_With_Zero_If_The_First_Argument_Is_Help()
40+
{
41+
CommandDispatcherMock commandDispatcher = CommandDispatcherMock.Create(new[] {"-h"});
42+
43+
commandDispatcher.Run();
44+
45+
Assert.Equal(0, _environmentContextMock.ExitValue);
46+
Assert.NotNull(_consoleContextMock.Text);
47+
Assert.NotEmpty(_consoleContextMock.Text);
48+
}
49+
50+
[Fact]
51+
public void Run_Should_Write_Error_And_Exit_With_One_If_The_Arguments_Does_Not_Contains_Valid_ServiceName()
52+
{
53+
CommandDispatcherMock commandDispatcher = CommandDispatcherMock.Create(new[] { "-foo -bar" });
54+
55+
commandDispatcher.Run();
56+
57+
Assert.Equal(1, _environmentContextMock.ExitValue);
58+
Assert.NotNull(_consoleContextMock.Text);
59+
Assert.NotEmpty(_consoleContextMock.Text);
60+
Assert.StartsWith("ERROR:", _consoleContextMock.Text);
61+
}
62+
63+
[Fact]
64+
public void Run_Should_Write_Error_And_Exit_With_One_If_Given_ServiceName_Has_Not_Valid_LocalStack_Endpoint()
65+
{
66+
CommandDispatcherMock commandDispatcher = CommandDispatcherMock.Create(new[] { "hinesis", "list-streams" });
67+
68+
commandDispatcher.Config
69+
.Setup(config => config.GetAwsServiceEndpoints())
70+
.Returns(() => new List<AwsServiceEndpoint>());
71+
72+
commandDispatcher.Run();
73+
74+
Assert.Equal(1, _environmentContextMock.ExitValue);
75+
Assert.NotNull(_consoleContextMock.Text);
76+
Assert.NotEmpty(_consoleContextMock.Text);
77+
Assert.StartsWith("ERROR:", _consoleContextMock.Text);
78+
79+
commandDispatcher.Config
80+
.Verify(config => config.GetAwsServiceEndpoints(), Times.Once);
81+
}
82+
1283
[Fact]
13-
public void Run_Should_Write_Help_Info_And_Exit_If_Argument_Count_Zero()
84+
public void Run_Should_Run_Aws_Command_With_Generated_Command_And_Aws_Credentials()
1485
{
15-
CommandDispatcherMock commandDispatcherMock = CommandDispatcherMock.Create(new string[0]);
86+
var args = new[] {"kinesis", "list-streams"};
87+
88+
AwsServiceEndpointMetadata endpointMetadata = AwsServiceEndpointMetadata.Kinesis;
89+
var awsServiceEndpoint = new AwsServiceEndpoint(
90+
endpointMetadata.ServiceId,
91+
endpointMetadata.CliName,
92+
endpointMetadata.Enum,
93+
endpointMetadata.Port,
94+
"localhost",
95+
endpointMetadata.ToString("http", "localhost"));
96+
97+
string cliCommand = args.GetCliCommand(awsServiceEndpoint.ServiceUrl);
98+
99+
CommandDispatcherMock commandDispatcher = CommandDispatcherMock.Create(args);
100+
101+
102+
commandDispatcher.Config
103+
.Setup(config => config.GetAwsServiceEndpoints())
104+
.Returns(() => new[] {awsServiceEndpoint});
105+
106+
commandDispatcher.ProcessHelper
107+
.Setup(helper => helper.CmdExecute(
108+
It.IsAny<string>(),
109+
It.IsAny<string>(),
110+
It.IsAny<bool>(),
111+
It.IsAny<bool>(),
112+
It.IsAny<Dictionary<string, string>>()))
113+
.Returns(0);
114+
115+
commandDispatcher.Run();
16116

117+
commandDispatcher.Config
118+
.Verify(config => config.GetAwsServiceEndpoints(), Times.Once);
17119

120+
commandDispatcher.ProcessHelper
121+
.Verify(helper => helper.CmdExecute(
122+
It.Is<string>(s => s == cliCommand),
123+
It.Is<string>(s => s == null),
124+
It.Is<bool>(b => b),
125+
It.Is<bool>(b => b),
126+
It.IsAny<Dictionary<string, string>>()), Times.Once);
18127
}
19128
}
20129
}

0 commit comments

Comments
 (0)