Skip to content

Feature/info option #282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Dotnet.Script.Tests/EnvironmentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
using System.Text.RegularExpressions;


namespace Dotnet.Script.Tests
{
public class EnvironmentTests
{
[Fact]
public void ShouldPrintVersionNumber()
{
var result = ScriptTestRunner.Default.Execute("--version");
Assert.Equal(0, result.exitCode);
Assert.Matches(@"\d*.\d*.\d*", result.output);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an incorrect test. It says match 3 Unicode digit separated by anything and which appears anywhere in the output. What's more, since the Unicode digits are optional (*), it will also match 3 of anything! The right test would be:

Assert.Matches(@"^[0-9]+(\.[0-9]+){2}$", result.output);

However, this test is still somewhat inaccurate. If you run dotnet script --version, the output is:


0.23.0

The trouble here is that there is a leading blank line! The test shouldn't be passing but looks like it does because ProcessHelper.RunAndCaptureOutput trims the output:

https://github.com/filipw/dotnet-script/blob/9bced12676415fd5eb1c69b95cf3765c71190358/src/Dotnet.Script.Tests/ProcessHelper.cs#L40

Ideally trimming should be decision of the caller.


result = ScriptTestRunner.Default.Execute("-v");
Assert.Equal(0, result.exitCode);
Assert.Matches(@"\d*.\d*.\d*", result.output);
}

[Fact]
public void ShouldPrintInfo()
{
var result = ScriptTestRunner.Default.Execute("--info");
Assert.Equal(0, result.exitCode);
Assert.Contains("Version", result.output);
Assert.Contains("Install location", result.output);
Assert.Contains("Target framework", result.output);
Assert.Contains("Platform identifier", result.output);
Assert.Contains("Runtime identifier", result.output);
}

}
}
66 changes: 66 additions & 0 deletions src/Dotnet.Script.Tests/ScriptTestRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Dotnet.Script.DependencyModel.Environment;

namespace Dotnet.Script.Tests
{
public class ScriptTestRunner
{
public static readonly ScriptTestRunner Default = new ScriptTestRunner();

private ScriptEnvironment _scriptEnvironment;

private ScriptTestRunner()
{
_scriptEnvironment = ScriptEnvironment.Default;
}


public (string output, int exitCode) Execute(params string[] arguments)
{
var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments(arguments));
return result;
}

public int ExecuteInProcess(params string[] arguments)
{
return Program.Main(arguments ?? Array.Empty<string>());
}

public (string output, int exitCode) ExecuteFixture(string fixture, params string[] arguments)
{
var pathToFixture = Path.Combine("..", "..", "..", "TestFixtures", fixture);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reuse GetPathToFixture here?

var result = ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments(new string[] { pathToFixture}.Concat(arguments ?? Array.Empty<string>()).ToArray()));
return result;
}

public int ExecuteFixtureInProcess(string fixture, params string[] arguments)
{
var pathToFixture = Path.Combine("..", "..", "..", "TestFixtures", fixture);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used. Can be removed as you call GetPathToFixture below.

return Program.Main(new[] { GetPathToFixture(fixture) }.Concat(arguments ?? Array.Empty<string>()).ToArray());
}

private static string GetPathToFixture(string fixture)
{
return Path.Combine("..", "..", "..", "TestFixtures", fixture);
}

private string[] GetDotnetScriptArguments(params string[] arguments)
{
string configuration;
#if DEBUG
configuration = "Debug";
#else
configuration = "Release";
#endif
var allArguments = new List<string>(new[] { "exec", Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", "..", "Dotnet.Script", "bin", configuration, _scriptEnvironment.TargetFramework, "dotnet-script.dll")});
if (arguments != null)
{
allArguments.AddRange(arguments);
}
return allArguments.ToArray();
}
}
}
26 changes: 19 additions & 7 deletions src/Dotnet.Script/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ private static int Wain(string[] args)

app.HelpOption("-? | -h | --help");

app.VersionOption("-v | --version", GetVersionInfo);
app.VersionOption("-v | --version", GetVersion());

var infoOption = app.Option("--info", "Displays environmental information", CommandOptionType.NoValue);

app.Command("eval", c =>
{
Expand Down Expand Up @@ -127,6 +129,13 @@ private static int Wain(string[] args)
app.OnExecute(async () =>
{
int exitCode = 0;

if (infoOption.HasValue())
{
Console.Write(GetEnvironmentInfo());
return 0;
}

if (!string.IsNullOrWhiteSpace(file.Value))
{
var optimizationLevel = OptimizationLevel.Debug;
Expand Down Expand Up @@ -207,18 +216,21 @@ private static Task<int> Run(bool debugMode, ScriptContext context)
return runner.Execute<int>(context);
}

private static string GetVersionInfo()
private static string GetEnvironmentInfo()
{
StringBuilder sb = new StringBuilder();
var versionAttribute = typeof(Program).GetTypeInfo().Assembly.GetCustomAttributes<AssemblyInformationalVersionAttribute>().SingleOrDefault();
sb.AppendLine($"Version : {versionAttribute?.InformationalVersion}");
StringBuilder sb = new StringBuilder();
sb.AppendLine($"Version : {GetVersion()}");
sb.AppendLine($"Install location : {ScriptEnvironment.Default.InstallLocation}");
sb.AppendLine($"Target framework : {ScriptEnvironment.Default.TargetFramework}");
sb.AppendLine($"Platform identifier : {ScriptEnvironment.Default.PlatformIdentifier}");
sb.AppendLine($"Runtime identifier : {ScriptEnvironment.Default.RuntimeIdentifier}");
return sb.ToString();
return sb.ToString();
}


private static string GetVersion()
{
var versionAttribute = typeof(Program).GetTypeInfo().Assembly.GetCustomAttributes<AssemblyInformationalVersionAttribute>().SingleOrDefault();
return versionAttribute?.InformationalVersion;
}

private static ScriptCompiler GetScriptCompiler(bool debugMode)
Expand Down