Skip to content

Commit

Permalink
Reintroduce --version option in a vswhere-compatible way
Browse files Browse the repository at this point in the history
We had disabled this in  9093d5a because it previously interfered with the ability to run vs where with a version specifier.

We now instead check that the version is defined as the *first* argument specified, which covers that and makes it so that only `vs --version` will show it.

Fixes #72
  • Loading branch information
kzu committed Jan 30, 2021
1 parent e2b932d commit abf6226
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
22 changes: 18 additions & 4 deletions src/VisualStudio.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -17,12 +17,11 @@ public ProgramTests(ITestOutputHelper output) =>


[Theory]
[InlineData(null)]
[InlineData("/help")]
[InlineData("/?")]
[InlineData("-?")]
[InlineData("/h")]
public async Task when_running_without_args_or_with_help_arg_then_usage_is_shown(params string[] args)
public async Task when_running_with_help_arg_then_usage_is_shown(params string[] args)
{
var program = new ProgramTest(output, new CommandFactory(), args ?? new string[0]);

Expand All @@ -32,7 +31,7 @@ public async Task when_running_without_args_or_with_help_arg_then_usage_is_shown
Assert.True(program.UsageShown);
}

[Fact(Skip = "--version disabled pending a redesign")]
[Fact]
public async Task when_running_with_version_arg_then_version_is_shown()
{
var program = new ProgramTest(output, new CommandFactory(), "--version");
Expand All @@ -43,6 +42,21 @@ public async Task when_running_with_version_arg_then_version_is_shown()
Assert.True(program.VersionShown);
}

[Fact]
public async Task when_running_where_command_with_version_arg_then_version_is_not_shown()
{
var command = Mock.Of<Command>();
var commandFactory = new CommandFactory();
commandFactory.RegisterCommand("test", () => Mock.Of<CommandDescriptor>(), x => command);
var program = new ProgramTest(output, commandFactory, "test", "--version");

var exitCode = await program.RunAsync();

Assert.Equal(0, exitCode);
Assert.False(program.VersionShown);
Mock.Get(command).Verify(x => x.ExecuteAsync(output));
}

[Fact]
public async Task when_running_command_then_command_is_executed()
{
Expand Down
21 changes: 11 additions & 10 deletions src/VisualStudio/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -40,19 +40,20 @@ public async Task CancelAsync()

public async Task<int> RunAsync()
{
if (args.Length == 0 || new[] { "?", "-?", "/?", "-h", "/h", "--help", "/help" }.Contains(args[0]))
if (args.Length != 0 && new[] { "?", "-?", "/?", "-h", "/h", "--help", "/help" }.Contains(args[0]))
{
ShowUsage();
return 0;
}
//else if (VersionOption.IsDefined(args))
//{
// ShowVersion();
// return 0;
//}

var commandName = args[0];
var commandArgs = ImmutableArray.Create(args.Skip(1).ToArray());
if (args.Length != 0 && VersionOption.IsDefined(new[] { args[0] }))
{
// Only consider --version if it's the *first* argument defined.
ShowVersion();
return 0;
}

var commandName = args.Length == 0 ? Commands.Run : args[0];
var commandArgs = ImmutableArray.Create(args.Length == 0 ? args : args.Skip(1).ToArray());
try
{
executingCommand = await commandFactory.CreateCommandAsync(commandName, commandArgs);
Expand Down

0 comments on commit abf6226

Please sign in to comment.