Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
nils-a committed Jun 22, 2021
2 parents f877025 + 290802b commit 6bf0b80
Show file tree
Hide file tree
Showing 45 changed files with 1,846 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
os: [ windows-2019 ]

env:
#COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
GITHUB_PAT: ${{ secrets.GH_TOKEN }}
GPR_PASSWORD: ${{ secrets.GPR_PASSWORD }}
GPR_USER: ${{ secrets.GPR_USER }}
Expand Down
4 changes: 3 additions & 1 deletion GitReleaseManager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ create:
footer-heading: Where to get it
footer-content: >
You can install this release via
`dotnet tool install -g JavaVersionSwitcher`.
`dotnet tool install -g JavaVersionSwitcher --version={milestone}`
or update via
`dotnet tool update -g JavaVersionSwitcher --version={milestone}`
footer-includes-milestone: true
milestone-replace-text: "{milestone}"
include-sha-section: true
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# JavaVersionSwitcher

[![standard-readme compliant][]][standard-readme]
[![Contributor Covenant][contrib-covenantimg]][contrib-covenant]
[![Build][githubimage]][githubbuild]
[![NuGet package][nugetimage]][nuget]

> .NET tool to make switching java versions on windows easy.
.NET tool to make switching java versions on windows easy.

## Table of Contents

Expand Down Expand Up @@ -56,15 +57,15 @@ We accept Pull Requests.

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

This project follows the [all-contributors][] specification. Contributions of any kind welcome!

## License

[MIT License © Nils Andresen][license]

[githubbuild]: https://github.com/nils-org/JavaVersionSwitcher/actions/workflows/build.yaml?query=branch%3Adevelop
[githubimage]: https://github.com/nils-org/JavaVersionSwitcher/actions/workflows/build.yaml/badge.svg?branch=develop
[maintainer]: https://github.com/nils-a
[contrib-covenant]: https://www.contributor-covenant.org/version/2/0/code_of_conduct/
[contrib-covenantimg]: https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg
[nuget]: https://nuget.org/packages/JavaVersionSwitcher
[nugetimage]: https://img.shields.io/nuget/v/JavaVersionSwitcher.svg?logo=nuget&style=flat-square
[license]: LICENSE.txt
Expand Down
2 changes: 0 additions & 2 deletions recipe.cake
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ BuildParameters.SetParameters(
shouldRunDotNetCorePack: true,
preferredBuildProviderType: BuildProviderType.GitHubActions,
twitterMessage: standardNotificationMessage,
shouldRunCoveralls: false, // no tests, currently
shouldRunCodecov: false,
shouldRunIntegrationTests: false);

BuildParameters.PrintParameters(Context);
Expand Down
124 changes: 124 additions & 0 deletions src/JavaVersionSwitcher.Tests/ConfigurationServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JavaVersionSwitcher.Tests.Fixtures;
using Shouldly;
using Xunit;

namespace JavaVersionSwitcher.Tests
{
public class ConfigurationServiceTests
{
[Fact]
public async Task SetConfiguration_throws_on_wrong_provider()
{
// arrange
using var fixture = new ConfigurationServiceFixture();
const string providerName = "non-existent-provider";

// act
// ReSharper disable once AccessToDisposedClosure
async Task Act() => await fixture.Service.SetConfiguration(providerName, null, null);

// assert
(await Should.ThrowAsync<KeyNotFoundException>((Func<Task>)Act))
.Message
.ShouldSatisfyAllConditions(
m => m.ShouldStartWith("No ConfigurationProvider"),
m => m.ShouldContain(providerName));
}

[Fact]
public async Task SetConfiguration_throws_on_wrong_setting()
{
// arrange
const string providerName = "provider";
using var fixture = new ConfigurationServiceFixture();
fixture.WithConfigurationProvider(providerName);
const string setting = "non-existent-setting";

// act'
// ReSharper disable once AccessToDisposedClosure
async Task Act() => await fixture.Service.SetConfiguration(providerName, setting, null);

// assert
(await Should.ThrowAsync<KeyNotFoundException>((Func<Task>)Act))
.Message
.ShouldSatisfyAllConditions(
m => m.ShouldStartWith("No Configuration with the name"),
m => m.ShouldContain(setting));
}

[Fact]
public async Task SetConfiguration_writes_value_to_xml()
{
// arrange
const string providerName = "pName";
const string settingsName = "settingsName";
const string value = "a value";
using var fixture = new ConfigurationServiceFixture();
fixture.WithConfigurationProvider(providerName, settingsName);

// act'
await fixture.Service.SetConfiguration(providerName, settingsName, value);

// assert
var xml = fixture.ReadXml(providerName, settingsName);
xml.Value.ShouldBe(value);
}

[Fact]
public async Task GetConfiguration_returns_empty_for_not_set_setting()
{
// arrange
const string providerName = "pName";
const string settingsName = "settingsName";
using var fixture = new ConfigurationServiceFixture();
fixture.WithConfigurationProvider(providerName, settingsName);

// act'
var actual = await fixture.Service.GetConfiguration(providerName, settingsName);

// assert
actual.ShouldBe(string.Empty);
}

[Fact]
public async Task GetConfiguration_returns_the_value_from_xml()
{
// arrange
const string providerName = "pName";
const string settingsName = "settingsName";
const string expected = "some value";
using var fixture = new ConfigurationServiceFixture();
fixture.WithConfigurationProvider(providerName, settingsName);
fixture.EnsureSetting(providerName, settingsName, expected);

// act'
var actual = await fixture.Service.GetConfiguration(providerName, settingsName);

// assert
actual.ShouldBe(expected);
}

[Fact]
public async Task SetConfiguration_removes_empty_settings()
{
// arrange
const string providerName = "pName";
const string settingsName = "settingsName";
using var fixture = new ConfigurationServiceFixture();
fixture.WithConfigurationProvider(providerName, settingsName);
fixture.EnsureSetting(providerName, settingsName, "some value");

// act'
await fixture.Service.SetConfiguration(providerName, settingsName, null);

// assert
var xml = fixture.ReadXml();
xml.Parent.ShouldBeNull("this should be the root node.");
xml.Elements().Count().ShouldBe(0);
}
}
}
58 changes: 58 additions & 0 deletions src/JavaVersionSwitcher.Tests/Fixtures/CommandFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using JavaVersionSwitcher.Adapters;
using JavaVersionSwitcher.Logging;
using JavaVersionSwitcher.Services;
using JavaVersionSwitcher.Tests.TestImplementations;
using Moq;
using SimpleInjector;
using Spectre.Console;
using Spectre.Console.Cli;
using Spectre.Console.Testing;

namespace JavaVersionSwitcher.Tests.Fixtures
{
public class CommandFixture
{
public TestConsole Console => new TestConsole();

public Logger Logger => new Logger();

public TestConfigurationService ConfigurationService => new TestConfigurationService();

public Mock<IJavaHomeAdapter> JavaHomeAdapter => new Mock<IJavaHomeAdapter>();

public Mock<IPathAdapter> PathAdapter => new Mock<IPathAdapter>();

public Mock<IJavaInstallationsAdapter> JavaInstallationsAdapter => new Mock<IJavaInstallationsAdapter>();

private ITypeRegistrar BuildRegistrar()
{
var container = new Container();
container.RegisterInstance<ILogger>(Logger);
container.RegisterInstance<IConfigurationService>(ConfigurationService);
container.RegisterInstance(JavaHomeAdapter.Object);
container.RegisterInstance(PathAdapter.Object);
container.RegisterInstance(JavaInstallationsAdapter.Object);

container.Register<JavaInstallationsAdapterConfigurationProvider>(Lifestyle.Singleton);

container.Collection.Register<IConfigurationProvider>(
new[]
{
typeof(JavaInstallationsAdapterConfigurationProvider),
},
Lifestyle.Singleton);

return new SimpleInjectorRegistrar(container);
}

public int Run(params string[] args)
{
AnsiConsole.Console = Console;
var registrar = BuildRegistrar();
var app = new CommandApp(registrar);
app.Configure(Program.ConfigureApp);

return app.Run(args);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using JavaVersionSwitcher.Adapters;
using JavaVersionSwitcher.Services;
using JetBrains.Annotations;
using Moq;
using Shouldly;

namespace JavaVersionSwitcher.Tests.Fixtures
{
public class ConfigurationServiceFixture : IDisposable
{
private readonly List<IConfigurationProvider> _configurationProviders = new List<IConfigurationProvider>();
private readonly Mock<IStorageAdapter> _storageAdapter;
private readonly string _tmpFile;

public ConfigurationServiceFixture()
{
_tmpFile = Path.GetTempFileName()+".xml";
_storageAdapter = new Mock<IStorageAdapter>();
_storageAdapter.Setup(x => x.ConfigurationFilePath).Returns(_tmpFile);
}

public ConfigurationService Service => new ConfigurationService(_configurationProviders, _storageAdapter.Object);

public void WithConfigurationProvider(string providerName, params string[] settings)
{
var configurationProvider = new Mock<IConfigurationProvider>();
configurationProvider.Setup(x => x.ProviderName).Returns(providerName);
configurationProvider.Setup(x => x.Settings).Returns(settings);

_configurationProviders.Add(configurationProvider.Object);
}

public void EnsureSetting([NotNull]string providerName, [NotNull]string setting, string value)
{
var doc = new XDocument();
doc.Add(ReadXml());
var root = doc.Root;

var providerElm = root!.Elements(providerName).SingleOrDefault();
if (providerElm == null)
{
providerElm = new XElement(providerName);
root.Add(providerElm);
}

var settingElm = providerElm.Elements(setting).SingleOrDefault();
if (settingElm == null)
{
settingElm = new XElement(setting);
providerElm.Add(settingElm);
}

settingElm.Value = value;
doc.Save(_tmpFile);
}

public XElement ReadXml(string providerName = null, string setting = null)
{
if (!File.Exists(_tmpFile))
{
return new XElement("temp-settings");
}

var xml = XDocument.Load(_tmpFile);
if (providerName == null)
{
return xml.Root;
}

var providerElm = xml.Root!.Elements(providerName).SingleOrDefault();
providerElm.ShouldNotBeNull("a provider element should have been created.");
var settingElm = providerElm.Elements(setting).SingleOrDefault();
if (setting == null)
{
return providerElm;
}

settingElm.ShouldNotBeNull("a settings element should have been created.");
return settingElm;
}

public void Dispose()
{
GC.SuppressFinalize(this);
if (_tmpFile != null && File.Exists(_tmpFile))
{
File.Delete(_tmpFile);
}
}
}
}
20 changes: 20 additions & 0 deletions src/JavaVersionSwitcher.Tests/GetConfigCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Xunit;

namespace JavaVersionSwitcher.Tests
{
public class GetConfigCommandTests
{
[Fact]
public void Can_Set_CacheTimeout_Configuration()
{
/*
var fixture = new CommandFixture();
var result = fixture.Run("config", "set", "cache", "timeout", "12");
result.ShouldBe(0);
fixture.ConfigurationService.Configuration["cache"]["timeout"].ShouldBe("12");
*/
}
}
}
Loading

0 comments on commit 6bf0b80

Please sign in to comment.