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

Fake.DotNet.Testing.SpecFlow: Added tests and optimized API #2143

Merged
merged 2 commits into from
Oct 11, 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
14 changes: 7 additions & 7 deletions help/markdown/fake-dotnet-testing-specflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ open Fake.DotNet.Testing
let specsProject = "IntegrationTests.csproj"

Target.create "Regenerate Test Classes" (fun _ ->
SpecFlowNext.run (fun p ->
{ p with ProjectFile = specsProject })
specsProject |> SpecFlowNext.run id
)

Target.create "Create StepDefinition Report" (fun _ ->
SpecFlowNext.run (fun p ->
{ p with SubCommand = StepDefinitionReport
ProjectFile = specsProject
BinFolder = Some "bin/Debug"
OutputFile = Some "StepDefinitionReport.html" })
specsProject
|> SpecFlowNext.run (fun p ->
{ p with
SubCommand = StepDefinitionReport
BinFolder = Some "bin/Debug"
OutputFile = Some "StepDefinitionReport.html" })
)

Target.create "Default" Target.DoNothing
Expand Down
30 changes: 17 additions & 13 deletions src/app/Fake.DotNet.Testing.SpecFlow/SpecFlowNext.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type SubCommand =
/// SpecFlow execution parameter type.
type SpecFlowParams = {
SubCommand: SubCommand
ProjectFile: string
ToolName: string
ToolPath: string
WorkingDir: string
Expand All @@ -45,7 +44,6 @@ let private currentDirectory = Directory.GetCurrentDirectory ()
/// SpecFlow default execution parameters.
let private SpecFlowDefaults = {
SubCommand = GenerateAll
ProjectFile = null
ToolName = toolname
ToolPath = Tools.findToolFolderInSubPath toolname (currentDirectory </> "tools" </> "SpecFlow")
WorkingDir = null
Expand All @@ -59,7 +57,12 @@ let private SpecFlowDefaults = {
XsltFile = None
}

let internal createProcess setParams =
let internal createProcess setParams projectFile =
if projectFile |> String.isNullOrWhiteSpace
then
Trace.traceError "SpecFlow needs a non empty project file!"
failwithf "SpecFlow needs a non empty project file!"
Copy link
Member

Choose a reason for hiding this comment

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

personally I'd probably only use failwithf (or invalidArg)


let parameters = setParams SpecFlowDefaults
let tool = parameters.ToolPath </> parameters.ToolName

Expand All @@ -74,9 +77,8 @@ let internal createProcess setParams =
[
yield parameters.SubCommand |> string

if not (isNull parameters.ProjectFile) then
yield "--ProjectFile"
yield parameters.ProjectFile
yield "--ProjectFile"
yield projectFile

yield! parameters.BinFolder
|> yieldIfSome "binFolder"
Expand All @@ -95,28 +97,30 @@ let internal createProcess setParams =
yield! parameters.FeatureLanguage
|> yieldIfSome "FeatureLanguage"

if parameters.Verbose then yield "verbose"
if parameters.ForceRegeneration then yield "force"
if parameters.Verbose then yield "--verbose"
if parameters.ForceRegeneration then yield "--force"

yield! parameters.XsltFile
|> yieldIfSome "XsltFile"
]
|> Arguments.OfArgs
//|> Args.toWindowsCommandLine

Trace.trace (tool + " " + args.ToStartInfo)

parameters,
CreateProcess.fromCommand (RawCommand(tool, args))
|> CreateProcess.withFramework
|> CreateProcess.withWorkingDirectory parameters.WorkingDir
|> CreateProcess.ensureExitCode
|> fun command ->
Trace.trace command.CommandLine
command

// Runs SpecFlow on a project.
/// ## Parameters
///
/// - `setParams` - Function used to manipulate the default SpecFlow parameter value.
let run setParams =
let parameters, cp = createProcess setParams
/// - `projectFile` - The required project file.
let run setParams projectFile =
let parameters, cp = projectFile |> createProcess setParams
use __ = Trace.traceTask "SpecFlow " (parameters.SubCommand |> string)
cp
|> Proc.run
Expand Down
73 changes: 54 additions & 19 deletions src/test/Fake.Core.UnitTests/Fake.DotNet.Testing.SpecFlow.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,65 @@ module Fake.DotNet.Testing.SpecFlowTests

open System.IO
open Fake.Core
open Fake.DotNet
open Fake.DotNet.Testing
open Fake.Testing
open Expecto

let runCreateProcess setParams =
let _, cp =
"projectfile.csproj"
|> SpecFlowNext.createProcess (fun param ->
{ setParams param with ToolPath = "specflow" })

let file, args =
match cp.Command with
| RawCommand(file, args) -> file, args
| _ -> failwithf "expected RawCommand"
|> ArgumentHelper.checkIfMono

let expectedPath = Path.Combine("specflow", "specflow.exe")
Expect.equal file expectedPath "Expected specflow.exe"

expectedPath, cp.Command.CommandLine

[<Tests>]
let tests =
testList "Fake.DotNet.Testing.SpecFlow.Tests" [
testCase "Test that new argument generation works" <| fun _ ->
let p, cp =
SpecFlowNext.createProcess (fun param ->
{ param with
ToolPath = "specflow"
SubCommand = SpecFlowNext.MsTestExecutionReport
})

let file, args =
match cp.Command with
| RawCommand(file, args) -> file, args
| _ -> failwithf "expected RawCommand"
//|> ArgumentHelper.checkIfMono
let expectedPath =
Path.Combine("specflow", "specflow.exe")
Expect.equal file expectedPath "Expected specflow.exe"
Expect.equal cp.Command.CommandLine
(sprintf "%s MsTestExecutionReport" expectedPath) "expected proper command line"
]
let expectedPath, commandLine =
runCreateProcess (fun param ->
{ param with
SubCommand = SpecFlowNext.MsTestExecutionReport })

Expect.equal commandLine
(sprintf "%s MsTestExecutionReport --ProjectFile projectfile.csproj" expectedPath) "expected proper command line"

testCase "Test that argument generation fails with exception if project file is not given" <| fun _ ->
Expect.throws
(fun _ -> SpecFlowNext.createProcess id "" |> ignore)
"expected to throw an exception"

testCase "Test that argument generation works with default arguments" <| fun _ ->
let expectedPath, commandLine =
runCreateProcess id

Expect.equal commandLine
(sprintf "%s GenerateAll --ProjectFile projectfile.csproj" expectedPath) "expected proper command line"

testCase "Test that argument generation works with all arguments set" <| fun _ ->
let expectedPath, commandLine =
runCreateProcess (fun param ->
{ param with
SubCommand = SpecFlowNext.NUnitExecutionReport
BinFolder = Some "bin/debug"
OutputFile = Some "output.html"
XmlTestResultFile = Some "testresult.xml"
TestOutputFile = Some "testoutput.txt"
FeatureLanguage = Some "de-DE"
Verbose = true
ForceRegeneration = true
XsltFile = Some "transform.xsl" })

Expect.equal commandLine
(sprintf "%s NUnitExecutionReport --ProjectFile projectfile.csproj --binFolder bin/debug --OutputFile output.html --xmlTestResult testresult.xml --testOutput testoutput.txt --FeatureLanguage de-DE --verbose --force --XsltFile transform.xsl" expectedPath ) "expected proper command line"
]