Skip to content
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

Added unit test for run being the default command #21

Merged
merged 1 commit into from
Apr 28, 2020
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
21 changes: 21 additions & 0 deletions VisualStudio.Tests/OutputHelperTextWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.IO;
using System.Text;
using Xunit.Abstractions;

namespace VisualStudio.Tests
{
internal class OutputHelperTextWriter : TextWriter
{
readonly ITestOutputHelper output;

public OutputHelperTextWriter(ITestOutputHelper output) => this.output = output;

public override Encoding Encoding => Encoding.UTF8;

public override void Write(string value) => WriteLine(value);

public override void WriteLine(string message) => output.WriteLine(message);

public override void WriteLine(string format, params object[] args) => output.WriteLine(format, args);
}
}
27 changes: 27 additions & 0 deletions VisualStudio.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace VisualStudio.Tests
{
public class ProgramTests
{
readonly TextWriter output;

public ProgramTests(ITestOutputHelper output) =>
this.output = new OutputHelperTextWriter(output);

[Fact]
public async Task when_no_command_specified_run_is_default()
{
var program = new Program(output, false, "pre");

var exit = await program.RunAsync();

Assert.Equal(0, exit);

Assert.IsType<RunCommand>(program.Command);
}
}
}
10 changes: 3 additions & 7 deletions VisualStudio/ConfigCommandDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ namespace VisualStudio
{
class ConfigCommandDescriptor : CommandDescriptor
{
readonly VisualStudioOptions options = new VisualStudioOptions(channelVerb: "Open", showNickname: false);
bool exp;
readonly VisualStudioOptions options = new VisualStudioOptions(channelVerb: "Open", showExp: true, showNickname: false);

public ConfigCommandDescriptor() => OptionSet = new CompositeOptionSet(options, new OptionSet
{
{ "exp", "Use experimental folder instead of regular.", e => exp = e != null },
});
public ConfigCommandDescriptor() => OptionSet = new CompositeOptionSet(options);

public Channel? Channel => options.Channel;

public Sku? Sku => options.Sku;

public bool Experimental => exp;
public bool Experimental => options.IsExperimental;
}
}
37 changes: 28 additions & 9 deletions VisualStudio/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Mono.Options;
Expand All @@ -7,34 +8,52 @@ namespace VisualStudio
{
class Program
{
static readonly CommandFactory commandFactory = new CommandFactory();

TextWriter output;
bool execute;
string[] args;

static async Task<int> Main(string[] args)
{
var commandFactory = new CommandFactory();

if (args.Length == 0 || new[] { "?", "-?", "/?", "-h", "/h", "--help", "/help" }.Contains(args[0]))
return ShowUsage(commandFactory);

return await new Program(Console.Out, true, args).RunAsync();
}

public Program(TextWriter output, bool execute, params string[] args)
{
this.output = output;
this.execute = execute;
this.args = args;

// Run is the default command if another one is not specified.
if (!commandFactory.IsCommandRegistered(args[0]))
args = args.Prepend("run").ToArray();
this.args = args.Prepend("run").ToArray();
}

public Command Command { get; private set; }

public async Task<int> RunAsync()
{
var commandName = args[0];
try
{
var command = commandFactory.CreateCommand(commandName, args.Skip(1));
Command = commandFactory.CreateCommand(commandName, args.Skip(1));

await command.ExecuteAsync(Console.Out);
// TODO: other exceptions might provide an exit code?
if (execute)
await Command.ExecuteAsync(output);
}
catch (ShowUsageException ex)
{
Console.WriteLine($"Usage: {ThisAssembly.Metadata.AssemblyName} {commandName} [options]");
ex.CommandDescriptor.ShowUsage(Console.Out);
output.WriteLine($"Usage: {ThisAssembly.Metadata.AssemblyName} {commandName} [options]");
ex.CommandDescriptor.ShowUsage(output);
return -1;
}
catch (OptionException ex)
{
Console.WriteLine(ex.Message);
output.WriteLine(ex.Message);
return -1;
}

Expand Down
4 changes: 2 additions & 2 deletions VisualStudio/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"VisualStudio": {
"commandName": "Project",
"commandLineArgs": "kill all exp"
"commandLineArgs": "config pre exp"
}
}
}
}