Skip to content

Commit 6bf0b80

Browse files
committed
Merge branch 'release/0.2.0'
2 parents f877025 + 290802b commit 6bf0b80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1846
-166
lines changed

.github/workflows/build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
os: [ windows-2019 ]
2323

2424
env:
25-
#COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
25+
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
2626
GITHUB_PAT: ${{ secrets.GH_TOKEN }}
2727
GPR_PASSWORD: ${{ secrets.GPR_PASSWORD }}
2828
GPR_USER: ${{ secrets.GPR_USER }}

GitReleaseManager.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ create:
33
footer-heading: Where to get it
44
footer-content: >
55
You can install this release via
6-
`dotnet tool install -g JavaVersionSwitcher`.
6+
`dotnet tool install -g JavaVersionSwitcher --version={milestone}`
7+
or update via
8+
`dotnet tool update -g JavaVersionSwitcher --version={milestone}`
79
footer-includes-milestone: true
810
milestone-replace-text: "{milestone}"
911
include-sha-section: true

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# JavaVersionSwitcher
22

33
[![standard-readme compliant][]][standard-readme]
4+
[![Contributor Covenant][contrib-covenantimg]][contrib-covenant]
45
[![Build][githubimage]][githubbuild]
56
[![NuGet package][nugetimage]][nuget]
67

7-
> .NET tool to make switching java versions on windows easy.
8+
.NET tool to make switching java versions on windows easy.
89

910
## Table of Contents
1011

@@ -56,15 +57,15 @@ We accept Pull Requests.
5657

5758
Small note: If editing the Readme, please conform to the [standard-readme][] specification.
5859

59-
This project follows the [all-contributors][] specification. Contributions of any kind welcome!
60-
6160
## License
6261

6362
[MIT License © Nils Andresen][license]
6463

6564
[githubbuild]: https://github.com/nils-org/JavaVersionSwitcher/actions/workflows/build.yaml?query=branch%3Adevelop
6665
[githubimage]: https://github.com/nils-org/JavaVersionSwitcher/actions/workflows/build.yaml/badge.svg?branch=develop
6766
[maintainer]: https://github.com/nils-a
67+
[contrib-covenant]: https://www.contributor-covenant.org/version/2/0/code_of_conduct/
68+
[contrib-covenantimg]: https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg
6869
[nuget]: https://nuget.org/packages/JavaVersionSwitcher
6970
[nugetimage]: https://img.shields.io/nuget/v/JavaVersionSwitcher.svg?logo=nuget&style=flat-square
7071
[license]: LICENSE.txt

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+
}

0 commit comments

Comments
 (0)