Skip to content

Commit 5e9760d

Browse files
committed
(#17) added some configurations
1 parent 05dfda2 commit 5e9760d

25 files changed

+895
-56
lines changed

recipe.cake

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ BuildParameters.SetParameters(
1414
shouldRunDotNetCorePack: true,
1515
preferredBuildProviderType: BuildProviderType.GitHubActions,
1616
twitterMessage: standardNotificationMessage,
17-
shouldRunCoveralls: false, // no tests, currently
18-
shouldRunCodecov: false,
1917
shouldRunIntegrationTests: false);
2018

2119
BuildParameters.PrintParameters(Context);
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using JavaVersionSwitcher.Tests.Fixtures;
6+
using Shouldly;
7+
using Xunit;
8+
9+
namespace JavaVersionSwitcher.Tests
10+
{
11+
public class ConfigurationServiceTests
12+
{
13+
[Fact]
14+
public async Task SetConfiguration_throws_on_wrong_provider()
15+
{
16+
// arrange
17+
using var fixture = new ConfigurationServiceFixture();
18+
const string providerName = "non-existent-provider";
19+
20+
// act
21+
// ReSharper disable once AccessToDisposedClosure
22+
async Task Act() => await fixture.Service.SetConfiguration(providerName, null, null);
23+
24+
// assert
25+
(await Should.ThrowAsync<KeyNotFoundException>((Func<Task>)Act))
26+
.Message
27+
.ShouldSatisfyAllConditions(
28+
m => m.ShouldStartWith("No ConfigurationProvider"),
29+
m => m.ShouldContain(providerName));
30+
}
31+
32+
[Fact]
33+
public async Task SetConfiguration_throws_on_wrong_setting()
34+
{
35+
// arrange
36+
const string providerName = "provider";
37+
using var fixture = new ConfigurationServiceFixture();
38+
fixture.WithConfigurationProvider(providerName);
39+
const string setting = "non-existent-setting";
40+
41+
// act'
42+
// ReSharper disable once AccessToDisposedClosure
43+
async Task Act() => await fixture.Service.SetConfiguration(providerName, setting, null);
44+
45+
// assert
46+
(await Should.ThrowAsync<KeyNotFoundException>((Func<Task>)Act))
47+
.Message
48+
.ShouldSatisfyAllConditions(
49+
m => m.ShouldStartWith("No Configuration with the name"),
50+
m => m.ShouldContain(setting));
51+
}
52+
53+
[Fact]
54+
public async Task SetConfiguration_writes_value_to_xml()
55+
{
56+
// arrange
57+
const string providerName = "pName";
58+
const string settingsName = "settingsName";
59+
const string value = "a value";
60+
using var fixture = new ConfigurationServiceFixture();
61+
fixture.WithConfigurationProvider(providerName, settingsName);
62+
63+
// act'
64+
await fixture.Service.SetConfiguration(providerName, settingsName, value);
65+
66+
// assert
67+
var xml = fixture.ReadXml(providerName, settingsName);
68+
xml.Value.ShouldBe(value);
69+
}
70+
71+
[Fact]
72+
public async Task GetConfiguration_returns_empty_for_not_set_setting()
73+
{
74+
// arrange
75+
const string providerName = "pName";
76+
const string settingsName = "settingsName";
77+
using var fixture = new ConfigurationServiceFixture();
78+
fixture.WithConfigurationProvider(providerName, settingsName);
79+
80+
// act'
81+
var actual = await fixture.Service.GetConfiguration(providerName, settingsName);
82+
83+
// assert
84+
actual.ShouldBe(string.Empty);
85+
}
86+
87+
[Fact]
88+
public async Task GetConfiguration_returns_the_value_from_xml()
89+
{
90+
// arrange
91+
const string providerName = "pName";
92+
const string settingsName = "settingsName";
93+
const string expected = "some value";
94+
using var fixture = new ConfigurationServiceFixture();
95+
fixture.WithConfigurationProvider(providerName, settingsName);
96+
fixture.EnsureSetting(providerName, settingsName, expected);
97+
98+
// act'
99+
var actual = await fixture.Service.GetConfiguration(providerName, settingsName);
100+
101+
// assert
102+
actual.ShouldBe(expected);
103+
}
104+
105+
[Fact]
106+
public async Task SetConfiguration_removes_empty_settings()
107+
{
108+
// arrange
109+
const string providerName = "pName";
110+
const string settingsName = "settingsName";
111+
using var fixture = new ConfigurationServiceFixture();
112+
fixture.WithConfigurationProvider(providerName, settingsName);
113+
fixture.EnsureSetting(providerName, settingsName, "some value");
114+
115+
// act'
116+
await fixture.Service.SetConfiguration(providerName, settingsName, null);
117+
118+
// assert
119+
var xml = fixture.ReadXml();
120+
xml.Parent.ShouldBeNull("this should be the root node.");
121+
xml.Elements().Count().ShouldBe(0);
122+
}
123+
}
124+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using JavaVersionSwitcher.Adapters;
2+
using JavaVersionSwitcher.Logging;
3+
using JavaVersionSwitcher.Services;
4+
using JavaVersionSwitcher.Tests.TestImplementations;
5+
using Moq;
6+
using SimpleInjector;
7+
using Spectre.Console;
8+
using Spectre.Console.Cli;
9+
using Spectre.Console.Testing;
10+
11+
namespace JavaVersionSwitcher.Tests.Fixtures
12+
{
13+
public class CommandFixture
14+
{
15+
public TestConsole Console => new TestConsole();
16+
17+
public Logger Logger => new Logger();
18+
19+
public TestConfigurationService ConfigurationService => new TestConfigurationService();
20+
21+
public Mock<IJavaHomeAdapter> JavaHomeAdapter => new Mock<IJavaHomeAdapter>();
22+
23+
public Mock<IPathAdapter> PathAdapter => new Mock<IPathAdapter>();
24+
25+
public Mock<IJavaInstallationsAdapter> JavaInstallationsAdapter => new Mock<IJavaInstallationsAdapter>();
26+
27+
private ITypeRegistrar BuildRegistrar()
28+
{
29+
var container = new Container();
30+
container.RegisterInstance<ILogger>(Logger);
31+
container.RegisterInstance<IConfigurationService>(ConfigurationService);
32+
container.RegisterInstance(JavaHomeAdapter.Object);
33+
container.RegisterInstance(PathAdapter.Object);
34+
container.RegisterInstance(JavaInstallationsAdapter.Object);
35+
36+
container.Register<JavaInstallationsAdapterConfigurationProvider>(Lifestyle.Singleton);
37+
38+
container.Collection.Register<IConfigurationProvider>(
39+
new[]
40+
{
41+
typeof(JavaInstallationsAdapterConfigurationProvider),
42+
},
43+
Lifestyle.Singleton);
44+
45+
return new SimpleInjectorRegistrar(container);
46+
}
47+
48+
public int Run(params string[] args)
49+
{
50+
AnsiConsole.Console = Console;
51+
var registrar = BuildRegistrar();
52+
var app = new CommandApp(registrar);
53+
app.Configure(Program.ConfigureApp);
54+
55+
return app.Run(args);
56+
}
57+
}
58+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Xml.Linq;
6+
using JavaVersionSwitcher.Adapters;
7+
using JavaVersionSwitcher.Services;
8+
using JetBrains.Annotations;
9+
using Moq;
10+
using Shouldly;
11+
12+
namespace JavaVersionSwitcher.Tests.Fixtures
13+
{
14+
public class ConfigurationServiceFixture : IDisposable
15+
{
16+
private readonly List<IConfigurationProvider> _configurationProviders = new List<IConfigurationProvider>();
17+
private readonly Mock<IStorageAdapter> _storageAdapter;
18+
private readonly string _tmpFile;
19+
20+
public ConfigurationServiceFixture()
21+
{
22+
_tmpFile = Path.GetTempFileName()+".xml";
23+
_storageAdapter = new Mock<IStorageAdapter>();
24+
_storageAdapter.Setup(x => x.ConfigurationFilePath).Returns(_tmpFile);
25+
}
26+
27+
public ConfigurationService Service => new ConfigurationService(_configurationProviders, _storageAdapter.Object);
28+
29+
public void WithConfigurationProvider(string providerName, params string[] settings)
30+
{
31+
var configurationProvider = new Mock<IConfigurationProvider>();
32+
configurationProvider.Setup(x => x.ProviderName).Returns(providerName);
33+
configurationProvider.Setup(x => x.Settings).Returns(settings);
34+
35+
_configurationProviders.Add(configurationProvider.Object);
36+
}
37+
38+
public void EnsureSetting([NotNull]string providerName, [NotNull]string setting, string value)
39+
{
40+
var doc = new XDocument();
41+
doc.Add(ReadXml());
42+
var root = doc.Root;
43+
44+
var providerElm = root!.Elements(providerName).SingleOrDefault();
45+
if (providerElm == null)
46+
{
47+
providerElm = new XElement(providerName);
48+
root.Add(providerElm);
49+
}
50+
51+
var settingElm = providerElm.Elements(setting).SingleOrDefault();
52+
if (settingElm == null)
53+
{
54+
settingElm = new XElement(setting);
55+
providerElm.Add(settingElm);
56+
}
57+
58+
settingElm.Value = value;
59+
doc.Save(_tmpFile);
60+
}
61+
62+
public XElement ReadXml(string providerName = null, string setting = null)
63+
{
64+
if (!File.Exists(_tmpFile))
65+
{
66+
return new XElement("temp-settings");
67+
}
68+
69+
var xml = XDocument.Load(_tmpFile);
70+
if (providerName == null)
71+
{
72+
return xml.Root;
73+
}
74+
75+
var providerElm = xml.Root!.Elements(providerName).SingleOrDefault();
76+
providerElm.ShouldNotBeNull("a provider element should have been created.");
77+
var settingElm = providerElm.Elements(setting).SingleOrDefault();
78+
if (setting == null)
79+
{
80+
return providerElm;
81+
}
82+
83+
settingElm.ShouldNotBeNull("a settings element should have been created.");
84+
return settingElm;
85+
}
86+
87+
public void Dispose()
88+
{
89+
GC.SuppressFinalize(this);
90+
if (_tmpFile != null && File.Exists(_tmpFile))
91+
{
92+
File.Delete(_tmpFile);
93+
}
94+
}
95+
}
96+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Xunit;
2+
3+
namespace JavaVersionSwitcher.Tests
4+
{
5+
public class GetConfigCommandTests
6+
{
7+
[Fact]
8+
public void Can_Set_CacheTimeout_Configuration()
9+
{
10+
/*
11+
var fixture = new CommandFixture();
12+
13+
var result = fixture.Run("config", "set", "cache", "timeout", "12");
14+
15+
result.ShouldBe(0);
16+
fixture.ConfigurationService.Configuration["cache"]["timeout"].ShouldBe("12");
17+
*/
18+
}
19+
}
20+
}

src/JavaVersionSwitcher.Tests/JavaVersionSwitcher.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
1010
<PackageReference Include="Moq" Version="4.16.1" />
1111
<PackageReference Include="Shouldly" Version="4.0.3" />
12+
<PackageReference Include="Spectre.Console.Testing" Version="0.40.0" />
1213
<PackageReference Include="xunit" Version="2.4.1" />
1314
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
1415
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using JavaVersionSwitcher.Logging;
2+
using Shouldly;
3+
using Spectre.Console;
4+
using Spectre.Console.Testing;
5+
using Xunit;
6+
7+
namespace JavaVersionSwitcher.Tests
8+
{
9+
public class LoggerTests
10+
{
11+
private readonly TestConsole _console = new TestConsole();
12+
private readonly ILogger _logger = new Logger();
13+
14+
public LoggerTests()
15+
{
16+
AnsiConsole.Console = _console;
17+
}
18+
19+
[Fact]
20+
public void Writes_warning_with_prefix()
21+
{
22+
_logger.LogWarning("test");
23+
24+
_console.Output.ShouldStartWith("WARNING:");
25+
}
26+
27+
[Fact]
28+
public void Writes_verbose_when_verbose_is_set()
29+
{
30+
_logger.PrintVerbose = true;
31+
_logger.LogVerbose("test");
32+
33+
_console.Output.ShouldBe("test\n");
34+
}
35+
36+
[Fact]
37+
public void Writes_nothing_when_verbose_is_not_set()
38+
{
39+
_logger.PrintVerbose = false;
40+
_logger.LogVerbose("test");
41+
42+
_console.Output.ShouldBe(string.Empty);
43+
}
44+
}
45+
}

src/JavaVersionSwitcher.Tests/PathTests.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
using System.Linq;
33
using System.Threading.Tasks;
44
using JavaVersionSwitcher.Adapters;
5-
using JavaVersionSwitcher.Logging;
6-
using Moq;
75
using Shouldly;
86
using Xunit;
97

108
namespace JavaVersionSwitcher.Tests
119
{
1210
public class PathTests
1311
{
14-
private readonly PathAdapter _adapter = new PathAdapter(new Mock<ILogger>().Object);
12+
private readonly PathAdapter _adapter = new PathAdapter();
1513

1614
[Fact]
1715
public async Task Can_Set_per_process()

0 commit comments

Comments
 (0)