Skip to content

Commit 26e7954

Browse files
authored
Giraffe tests (wip) (#68)
1 parent 8a1b035 commit 26e7954

24 files changed

+1759
-96
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
]
1010
},
1111
"fable": {
12-
"version": "4.0.0-snake-island-alpha-024",
12+
"version": "4.0.0-snake-island-alpha-025",
1313
"commands": [
1414
"fable"
1515
]

Build.fs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ let cliPath = Path.getFullName "../Fable/src/Fable.Cli"
1515

1616
Target.create "Clean" (fun _ ->
1717
Shell.cleanDir buildPath
18-
// run dotnet "fable clean --yes" buildPath // Delete *.py files created by Fable
19-
// run dotnet $"run -c Release -p {cliPath} -- clean --yes --lang Python " buildPath
2018
)
2119

2220
Target.create "Build" (fun _ ->

examples/giraffe/Build.fs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
open Fake.Core
2+
open Fake.IO
3+
4+
open Helpers
5+
6+
initializeContext()
7+
8+
let buildPath = Path.getFullName "build"
9+
let srcPath = Path.getFullName "src"
10+
let deployPath = Path.getFullName "deploy"
11+
let testsPath = Path.getFullName "test"
12+
13+
// Until Fable (beyond) is released, we need to compile with locally installed Fable branch.
14+
let cliPath = Path.getFullName "../Fable/src/Fable.Cli"
15+
16+
Target.create "Clean" (fun _ ->
17+
Shell.cleanDir buildPath
18+
)
19+
20+
Target.create "Build" (fun _ ->
21+
Shell.mkdir buildPath
22+
run dotnet $"fable --exclude Fable.Core --lang Python --outDir {buildPath}" srcPath
23+
)
24+
25+
Target.create "Run" (fun _ ->
26+
run dotnet "build" srcPath
27+
)
28+
29+
Target.create "Test" (fun _ ->
30+
run dotnet "build" testsPath
31+
[ "native", dotnet "run" testsPath
32+
"python", dotnet $"fable --lang Python --outDir {buildPath}/tests" testsPath
33+
]
34+
|> runParallel
35+
run poetry $"run python -m pytest {buildPath}/tests" ""
36+
)
37+
38+
Target.create "Pack" (fun _ ->
39+
run dotnet "pack -c Release" srcPath
40+
)
41+
42+
Target.create "Format" (fun _ ->
43+
run dotnet "fantomas . -r" srcPath
44+
run dotnet "fantomas . -r" testsPath
45+
)
46+
47+
open Fake.Core.TargetOperators
48+
49+
let dependencies = [
50+
"Clean"
51+
==> "Build"
52+
53+
"Clean"
54+
==> "Run"
55+
56+
"Build"
57+
==> "Test"
58+
59+
"Build"
60+
==> "Pack"
61+
]
62+
63+
[<EntryPoint>]
64+
let main args = runOrDefault args

examples/giraffe/Build.fsproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="Helpers.fs" />
8+
<Compile Include="Build.fs" />
9+
</ItemGroup>
10+
<ItemGroup>
11+
<PackageReference Include="Fake.Core.Target" Version="5.23.0" />
12+
<PackageReference Include="Fake.IO.Filesystem" Version="5.23.0" />
13+
<PackageReference Include="System.Collections.Immutable" Version="6.0.0" />
14+
</ItemGroup>
15+
</Project>

examples/giraffe/Helpers.fs

Lines changed: 109 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,109 @@
1-
namespace Giraffe
2-
3-
[<AutoOpen>]
4-
module Helpers =
5-
open System
6-
open System.IO
7-
8-
/// <summary>
9-
/// Checks if an object is not null.
10-
/// </summary>
11-
/// <param name="x">The object to validate against `null`.</param>
12-
/// <returns>Returns true if the object is not null otherwise false.</returns>
13-
let inline isNotNull x = not (isNull x)
14-
15-
/// <summary>
16-
/// Converts a string into a string option where null or an empty string will be converted to None and everything else to Some string.
17-
/// </summary>
18-
/// <param name="str">The string value to be converted into an option of string.</param>
19-
/// <returns>Returns None if the string was null or empty otherwise Some string.</returns>
20-
let inline strOption (str : string) =
21-
if String.IsNullOrEmpty str then None else Some str
22-
23-
/// <summary>
24-
/// Reads a file asynchronously from the file system.
25-
/// </summary>
26-
/// <param name="filePath">The absolute path of the file.</param>
27-
/// <returns>Returns the string contents of the file wrapped in a Task.</returns>
28-
// let readFileAsStringAsync (filePath : string) =
29-
// task {
30-
// use reader = new StreamReader(filePath)
31-
// return! reader.ReadToEndAsync()
32-
// }
1+
module Helpers
2+
3+
open Fake.Core
4+
5+
let initializeContext () =
6+
let execContext = Context.FakeExecutionContext.Create false "build.fsx" [ ]
7+
Context.setExecutionContext (Context.RuntimeContext.Fake execContext)
8+
9+
module Proc =
10+
module Parallel =
11+
open System
12+
13+
let locker = obj()
14+
15+
let colors =
16+
[| ConsoleColor.Blue
17+
ConsoleColor.Yellow
18+
ConsoleColor.Magenta
19+
ConsoleColor.Cyan
20+
ConsoleColor.DarkBlue
21+
ConsoleColor.DarkYellow
22+
ConsoleColor.DarkMagenta
23+
ConsoleColor.DarkCyan |]
24+
25+
let print color (colored: string) (line: string) =
26+
lock locker
27+
(fun () ->
28+
let currentColor = Console.ForegroundColor
29+
Console.ForegroundColor <- color
30+
Console.Write colored
31+
Console.ForegroundColor <- currentColor
32+
Console.WriteLine line)
33+
34+
let onStdout index name (line: string) =
35+
let color = colors.[index % colors.Length]
36+
if isNull line then
37+
print color $"{name}: --- END ---" ""
38+
else if String.isNotNullOrEmpty line then
39+
print color $"{name}: " line
40+
41+
let onStderr name (line: string) =
42+
let color = ConsoleColor.Red
43+
if isNull line |> not then
44+
print color $"{name}: " line
45+
46+
let redirect (index, (name, createProcess)) =
47+
createProcess
48+
|> CreateProcess.redirectOutputIfNotRedirected
49+
|> CreateProcess.withOutputEvents (onStdout index name) (onStderr name)
50+
51+
let printStarting indexed =
52+
for (index, (name, c: CreateProcess<_>)) in indexed do
53+
let color = colors.[index % colors.Length]
54+
let wd =
55+
c.WorkingDirectory
56+
|> Option.defaultValue ""
57+
let exe = c.Command.Executable
58+
let args = c.Command.Arguments.ToStartInfo
59+
print color $"{name}: {wd}> {exe} {args}" ""
60+
61+
let run cs =
62+
cs
63+
|> Seq.toArray
64+
|> Array.indexed
65+
|> fun x -> printStarting x; x
66+
|> Array.map redirect
67+
|> Array.Parallel.map Proc.run
68+
69+
let createProcess exe arg dir =
70+
CreateProcess.fromRawCommandLine exe arg
71+
|> CreateProcess.withWorkingDirectory dir
72+
|> CreateProcess.ensureExitCode
73+
74+
let dotnet = createProcess "dotnet"
75+
76+
77+
let pytest = createProcess "pytest"
78+
let poetry = createProcess "poetry"
79+
80+
let npm =
81+
let npmPath =
82+
match ProcessUtils.tryFindFileOnPath "npm" with
83+
| Some path -> path
84+
| None ->
85+
"npm was not found in path. Please install it and make sure it's available from your path. " +
86+
"See https://safe-stack.github.io/docs/quickstart/#install-pre-requisites for more info"
87+
|> failwith
88+
89+
createProcess npmPath
90+
91+
let run proc arg dir =
92+
proc arg dir
93+
|> Proc.run
94+
|> ignore
95+
96+
let runParallel processes =
97+
processes
98+
|> Proc.Parallel.run
99+
|> ignore
100+
101+
let runOrDefault args =
102+
try
103+
match args with
104+
| [| target |] -> Target.runOrDefault target
105+
| _ -> Target.runOrDefault "Run"
106+
0
107+
with e ->
108+
printfn "%A" e
109+
1

examples/giraffe/Program.fs

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/giraffe/giraffe.sln

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30114.105
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Python", "src\Giraffe.Python.fsproj", "{6E973C60-9714-4F0D-A65B-60AB3DF3FADC}"
7+
EndProject
8+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Tests", "test\Giraffe.Tests.fsproj", "{D56D8E48-E02C-479D-A1F8-57352E3BD8AB}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(SolutionProperties) = preSolution
16+
HideSolutionNode = FALSE
17+
EndGlobalSection
18+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{6E973C60-9714-4F0D-A65B-60AB3DF3FADC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{6E973C60-9714-4F0D-A65B-60AB3DF3FADC}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{6E973C60-9714-4F0D-A65B-60AB3DF3FADC}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{6E973C60-9714-4F0D-A65B-60AB3DF3FADC}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{D56D8E48-E02C-479D-A1F8-57352E3BD8AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{D56D8E48-E02C-479D-A1F8-57352E3BD8AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{D56D8E48-E02C-479D-A1F8-57352E3BD8AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{D56D8E48-E02C-479D-A1F8-57352E3BD8AB}.Release|Any CPU.Build.0 = Release|Any CPU
27+
EndGlobalSection
28+
EndGlobal

0 commit comments

Comments
 (0)