2017-11-22 04:10:06 +00:00
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using FluentAssertions;
|
2018-01-13 17:40:48 +00:00
|
|
|
using Microsoft.DotNet.Tools;
|
2017-11-22 04:10:06 +00:00
|
|
|
using NuGet.Protocol.Core.Types;
|
|
|
|
using Xunit;
|
|
|
|
|
2017-12-04 22:13:24 +00:00
|
|
|
namespace Microsoft.DotNet.ToolPackage.Tests
|
2017-11-22 04:10:06 +00:00
|
|
|
{
|
|
|
|
public class ToolConfigurationDeserializerTests
|
|
|
|
{
|
|
|
|
[Fact]
|
|
|
|
public void GivenXmlPathItShouldGetToolConfiguration()
|
|
|
|
{
|
2017-12-04 22:13:24 +00:00
|
|
|
ToolConfiguration toolConfiguration = ToolConfigurationDeserializer.Deserialize("DotnetToolSettingsGolden.xml");
|
2017-11-22 04:10:06 +00:00
|
|
|
|
|
|
|
toolConfiguration.CommandName.Should().Be("sayhello");
|
|
|
|
toolConfiguration.ToolAssemblyEntryPoint.Should().Be("console.dll");
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GivenMalformedPathItThrows()
|
|
|
|
{
|
2017-12-04 22:13:24 +00:00
|
|
|
Action a = () => ToolConfigurationDeserializer.Deserialize("DotnetToolSettingsMalformed.xml");
|
2017-11-22 04:10:06 +00:00
|
|
|
a.ShouldThrow<ToolConfigurationException>()
|
|
|
|
.And.Message.Should()
|
2018-01-13 17:40:48 +00:00
|
|
|
.Contain(string.Format(CommonLocalizableStrings.ToolSettingsInvalidXml, string.Empty));
|
2017-11-22 04:10:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GivenMissingContentItThrows()
|
|
|
|
{
|
2017-12-04 22:13:24 +00:00
|
|
|
Action a = () => ToolConfigurationDeserializer.Deserialize("DotnetToolSettingsMissing.xml");
|
2017-11-22 04:10:06 +00:00
|
|
|
a.ShouldThrow<ToolConfigurationException>()
|
|
|
|
.And.Message.Should()
|
2018-01-18 03:26:52 +00:00
|
|
|
.Contain(CommonLocalizableStrings.ToolSettingsMissingCommandName);
|
2017-11-22 04:10:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 19:50:09 +00:00
|
|
|
[Fact]
|
|
|
|
public void GivenMissingVersionItHasWarningReflectIt()
|
|
|
|
{
|
|
|
|
ToolConfiguration toolConfiguration = ToolConfigurationDeserializer.Deserialize("DotnetToolSettingsMissingVersion.xml");
|
|
|
|
|
|
|
|
toolConfiguration.Warnings.First().Should().Be(CommonLocalizableStrings.FormatVersionIsMissing);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GivenMajorHigherVersionItHasWarningReflectIt()
|
|
|
|
{
|
|
|
|
ToolConfiguration toolConfiguration = ToolConfigurationDeserializer.Deserialize("DotnetToolSettingsMajorHigherVersion.xml");
|
|
|
|
|
|
|
|
toolConfiguration.Warnings.First().Should().Be(CommonLocalizableStrings.FormatVersionIsHigher);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GivenMinorHigherVersionItHasNoWarning()
|
|
|
|
{
|
|
|
|
ToolConfiguration toolConfiguration = ToolConfigurationDeserializer.Deserialize("DotnetToolSettingsGolden.xml");
|
|
|
|
|
|
|
|
toolConfiguration.Warnings.Should().BeEmpty();
|
|
|
|
}
|
|
|
|
|
2017-11-22 04:10:06 +00:00
|
|
|
[Fact]
|
|
|
|
public void GivenInvalidCharAsFileNameItThrows()
|
|
|
|
{
|
2018-01-18 03:26:52 +00:00
|
|
|
var invalidCommandName = "na\0me";
|
|
|
|
Action a = () => new ToolConfiguration(invalidCommandName, "my.dll");
|
2018-01-28 21:35:04 +00:00
|
|
|
a.ShouldThrow<ToolConfigurationException>()
|
2017-11-22 04:10:06 +00:00
|
|
|
.And.Message.Should()
|
2018-01-18 03:26:52 +00:00
|
|
|
.Contain(
|
|
|
|
string.Format(
|
|
|
|
CommonLocalizableStrings.ToolSettingsInvalidCommandName,
|
|
|
|
invalidCommandName,
|
|
|
|
string.Join(", ", Path.GetInvalidFileNameChars().Select(c => $"'{c}'"))));
|
2017-11-22 04:10:06 +00:00
|
|
|
}
|
2018-03-06 23:58:05 +00:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void GivenALeadingDotAsFileNameItThrows()
|
|
|
|
{
|
|
|
|
var invalidCommandName = ".mytool";
|
|
|
|
Action a = () => new ToolConfiguration(invalidCommandName, "my.dll");
|
|
|
|
a.ShouldThrow<ToolConfigurationException>()
|
|
|
|
.And.Message.Should()
|
|
|
|
.Contain(string.Format(
|
|
|
|
CommonLocalizableStrings.ToolSettingsInvalidLeadingDotCommandName,
|
|
|
|
invalidCommandName));
|
|
|
|
}
|
2017-11-22 04:10:06 +00:00
|
|
|
}
|
|
|
|
}
|