Skip to content

Commit

Permalink
Further argument processing, with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveGilham committed Jan 24, 2018
1 parent d3cd4ec commit ff4d0cd
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 9 deletions.
112 changes: 111 additions & 1 deletion AltCover.Runner.Tests/Runner.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ type AltCoverTests() = class
Runner.executable := None)

[<Test>]
member self.ParsingMoExeGivesFailure() =
member self.ParsingNoExeGivesFailure() =
lock Runner.executable (fun () ->
try
Runner.executable := None
Expand Down Expand Up @@ -447,6 +447,116 @@ type AltCoverTests() = class
finally
Runner.recordingDirectory <- None

[<Test>]
member self.ShouldRequireExe() =
lock Runner.executable (fun () ->
try
Runner.executable := None
let options = Runner.DeclareOptions ()
let parse = Runner.RequireExe (Right ([], options))
match parse with
| Right _ -> Assert.Fail()
| Left (x, y) -> Assert.That (y, Is.SameAs options)
Assert.That (x, Is.EqualTo "UsageError")
finally
Runner.executable := None)

[<Test>]
member self.ShouldAcceptExe() =
lock Runner.executable (fun () ->
try
Runner.executable := Some "xxx"
let options = Runner.DeclareOptions ()
let parse = Runner.RequireExe (Right (["b"], options))
match parse with
| Right (x::y, z) -> Assert.That (z, Is.SameAs options)
Assert.That (x, Is.EqualTo "xxx")
Assert.That (y, Is.EquivalentTo ["b"])
| _ -> Assert.Fail()
finally
Runner.executable := None)

[<Test>]
member self.ShouldRequireWorker() =
try
Runner.workingDirectory <- None
let options = Runner.DeclareOptions ()
let input = (Right ([], options))
let parse = Runner.RequireWorker input
match parse with
| Right _ -> Assert.That(parse, Is.SameAs input)
Assert.That(Option.isSome Runner.workingDirectory)
| _-> Assert.Fail()
finally
Runner.workingDirectory <- None

[<Test>]
member self.ShouldAcceptWorker() =
try
Runner.workingDirectory <- Some "ShouldAcceptWorker"
let options = Runner.DeclareOptions ()
let input = (Right ([], options))
let parse = Runner.RequireWorker input
match parse with
| Right _ -> Assert.That(parse, Is.SameAs input)
Assert.That(Runner.workingDirectory,
Is.EqualTo (Some "ShouldAcceptWorker"))
| _-> Assert.Fail()
finally
Runner.workingDirectory <- None

[<Test>]
member self.ShouldRequireRecorder() =
try
Runner.recordingDirectory <- None
let options = Runner.DeclareOptions ()
let input = (Right ([], options))
let parse = Runner.RequireRecorder input
match parse with
| Right _ -> Assert.Fail()
| Left (x, y) -> Assert.That (y, Is.SameAs options)
Assert.That (x, Is.EqualTo "UsageError")
finally
Runner.recordingDirectory <- None

[<Test>]
member self.ShouldRequireRecorderDll() =
try
let where = Assembly.GetExecutingAssembly().Location
let path = Path.Combine(where.Substring(0, where.IndexOf("_Binaries")), "_Mono/Sample1")
let path' = if Directory.Exists path then path
else Path.Combine(where.Substring(0, where.IndexOf("_Binaries")), "../_Mono/Sample1")
Runner.recordingDirectory <- Some path'
let options = Runner.DeclareOptions ()
let input = (Right ([], options))
let parse = Runner.RequireRecorder input
match parse with
| Right _ -> Assert.Fail()
| Left (x, y) -> Assert.That (y, Is.SameAs options)
Assert.That (x, Is.EqualTo "UsageError")
finally
Runner.recordingDirectory <- None
[<Test>]
member self.ShouldAcceptRecorder() =
try
let where = Assembly.GetExecutingAssembly().Location |> Path.GetDirectoryName
Runner.recordingDirectory <- Some where
let create = Path.Combine(where, "AltCover.Recorder.g.dll")
if create |> File.Exists |> not then do
let from = Path.Combine(where, "AltCover.Recorder.dll")
use frombytes = new FileStream(from, FileMode.Open, FileAccess.Read)
use libstream = new FileStream(create, FileMode.Create)
frombytes.CopyTo libstream

let options = Runner.DeclareOptions ()
let input = (Right ([], options))
let parse = Runner.RequireRecorder input
match parse with
| Right _ -> Assert.That(parse, Is.SameAs input)
| _-> Assert.Fail()
finally
Runner.recordingDirectory <- None

[<Test>]
member self.ShouldProcessTrailingArguments() =
// Hack for running while instrumented
Expand Down
39 changes: 31 additions & 8 deletions AltCover.Runner/Runner.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,43 @@ module Runner =
|> CommandLine.WriteErr
CommandLine.Usage intro options

let internal RequireExe (parse:(Either<string*OptionSet, string list*OptionSet>)) =
match parse with
| Right (l, options) -> match !executable with
| None -> Left ("UsageError", options)
| Some exe -> Right (exe::l, options)
| fail -> fail

let internal RequireRecorder (parse:(Either<string*OptionSet, string list*OptionSet>)) =
match parse with
| Right (_, options) -> match recordingDirectory with
| None -> Left ("UsageError", options)
| Some path -> let dll = Path.Combine (path, "AltCover.Recorder.g.dll")
if File.Exists dll then parse
else Left ("UsageError", options)
| fail -> fail

let internal RequireWorker (parse:(Either<string*OptionSet, string list*OptionSet>)) =
match parse with
| Right _ -> match workingDirectory with
| None -> workingDirectory <- Directory.GetCurrentDirectory() |> Some
| _ -> ()
parse
| fail -> fail

let DoCoverage arguments =
let check1 = DeclareOptions ()
|> CommandLine.ParseCommandLine arguments
|> CommandLine.ProcessHelpOption
|> RequireExe
|> RequireRecorder
|> RequireWorker
match check1 with
| Left (intro, options) -> HandleBadArguments arguments intro options
| Right (rest, options) ->
match !executable with
| None -> HandleBadArguments arguments "UsageError" options
| Some exe ->
try
CommandLine.ProcessTrailingArguments (exe::rest) null
with
| :? IOException as x -> CommandLine.WriteErr x.Message
| Right (rest, _) ->
CommandLine.doPathOperation (fun () ->
CommandLine.ProcessTrailingArguments rest null
)

[<EntryPoint>]
let private Main arguments =
Expand Down
4 changes: 4 additions & 0 deletions AltCover.Runner/altcover.runner.core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<PackageReference Include="Mono.Options" Version="5.3.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AltCover.Recorder\altcover.recorder.core.fsproj" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Strings.eo.resx">
<Generator>ResXFileCodeGenerator</Generator>
Expand Down

0 comments on commit ff4d0cd

Please sign in to comment.