Skip to content

Commit

Permalink
Merge pull request #623 from SAFE-Stack/run-project-tests-as-part-of-…
Browse files Browse the repository at this point in the history
…template-testing

Run project tests as part of template testing
  • Loading branch information
mattgallagher92 authored Jul 26, 2024
2 parents 41c524f + 088f59a commit 304f192
Show file tree
Hide file tree
Showing 8 changed files with 1,174 additions and 24 deletions.
13 changes: 11 additions & 2 deletions Build.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let skipTests = Environment.hasEnvironVar "yolo"
let release = ReleaseNotes.load "RELEASE_NOTES.md"

let templatePath = "./Content/.template.config/template.json"
let defaultTemplatePath = "./Content/default"
let templateProj = "SAFE.Template.proj"
let templateName = "SAFE-Stack Web App"
let version = Environment.environVarOrDefault "VERSION" ""
Expand Down Expand Up @@ -77,13 +78,20 @@ let run exe arg dir =
let result = Process.execWithResult (psi exe arg dir) TimeSpan.MaxValue
if not result.OK then (failwithf "`%s %s` failed: %A" exe arg result.Errors)

Target.create "Tests" (fun _ ->
Target.create "TemplateExecutionTests" (fun _ ->
let cmd = "run"
let args = "--project tests/Tests.fsproj"
let result = DotNet.exec (fun x -> { x with DotNetCliPath = "dotnet" }) cmd args
if not result.OK then failwithf "`dotnet %s %s` failed" cmd args
)

Target.create "DefaultTemplateTests" (fun _ ->
let cmd = "run"
let args = "RunTestsHeadless --project Build.fsproj"
let result = DotNet.exec (fun x -> { x with DotNetCliPath = "dotnet"; WorkingDirectory = defaultTemplatePath}) cmd args
if not result.OK then failwithf "`dotnet %s %s` failed" cmd args
)

Target.create "Release" (fun _ ->
let nugetApiKey = Environment.environVarOrFail "NUGET_API_KEY"
let nugetArgs = $"""push "*.nupkg" --api-key {nugetApiKey} --source https://api.nuget.org/v3/index.json"""
Expand All @@ -93,7 +101,8 @@ Target.create "Release" (fun _ ->
open Fake.Core.TargetOperators

"Clean"
=?> ("Tests", not skipTests)
=?> ("TemplateExecutionTests", not skipTests)
=?> ("DefaultTemplateTests", not skipTests)
==> "Pack"
==> "Install"
==> "Release"
Expand Down
17 changes: 14 additions & 3 deletions Content/default/Build.fs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,18 @@ Target.create "Run" (fun _ ->
]
|> runParallel)

Target.create "RunTests" (fun _ ->
run dotnet [ "build" ] sharedTestsPath
let buildSharedTests () = run dotnet [ "build" ] sharedTestsPath

Target.create "RunTestsHeadless" (fun _ ->
buildSharedTests ()

run dotnet [ "run" ] serverTestsPath
run dotnet [ "fable"; "-o"; "output" ] clientTestsPath
run npx [ "mocha"; "output" ] clientTestsPath
)

Target.create "WatchRunTests" (fun _ ->
buildSharedTests ()

[
"server", dotnet [ "watch"; "run" ] serverTestsPath
Expand All @@ -71,7 +81,8 @@ let dependencies = [

"Clean" ==> "RestoreClientDependencies" ==> "Run"

"RestoreClientDependencies" ==> "RunTests"
"RestoreClientDependencies" ==> "RunTestsHeadless"
"RestoreClientDependencies" ==> "WatchRunTests"
]

[<EntryPoint>]
Expand Down
12 changes: 8 additions & 4 deletions Content/default/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,20 @@ let createProcess exe args dir =

let dotnet args dir = createProcess "dotnet" args dir

let npm args dir =
let npmPath =
match ProcessUtils.tryFindFileOnPath "npm" with
let createProcessFromPath processName args dir =
let path =
match ProcessUtils.tryFindFileOnPath processName with
| Some path -> path
| None ->
"npm was not found in path. Please install it and make sure it's available from your path. "
+ "See https://safe-stack.github.io/docs/quickstart/#install-pre-requisites for more info"
|> failwith

createProcess npmPath args dir
createProcess path args dir

let npm args dir = createProcessFromPath "npm" args dir

let npx args dir = createProcessFromPath "npx" args dir

let run proc arg dir = proc arg dir |> Proc.run |> ignore

Expand Down
2 changes: 1 addition & 1 deletion Content/default/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The build project in root directory contains a couple of different build targets
To run concurrently server and client tests in watch mode (you can run this command in parallel to the previous one in new terminal):

```bash
dotnet run -- RunTests
dotnet run -- WatchRunTests
```

Client tests are available under `http://localhost:8081` in your browser and server tests are running in watch mode in console.
Expand Down
Loading

0 comments on commit 304f192

Please sign in to comment.