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

Forge integration #36

Closed
wants to merge 4 commits into from
Closed
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
66 changes: 66 additions & 0 deletions src/Mechanic.Tests/Forge.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module Forge.Tests

open System.IO
open Mechanic
open Expecto


let [<Literal>] ForgePath = "../paket-files/github.com/fsharp-editing/Forge/temp/Forge.exe"

let makeTempProject () =
let projectFileText = sprintf """<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="%s" />
<Compile Include="%s" />
</ItemGroup>
</Project>
"""
let tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())
Directory.CreateDirectory tempPath |> ignore
let pf = Path.Combine(tempPath, "TestProject.fsproj")
let f1 = Path.Combine(tempPath, "TestFile1.fs")
let f2 = Path.Combine(tempPath, "TestFile2.fs")
File.WriteAllText(pf, (projectFileText "TestFile1.fs" "TestFile2.fs"))
File.WriteAllText(f1, "namespace Test")
File.WriteAllText(f2, "namespace Test")
tempPath, pf, f1, f2


[<Tests>]
let tests =
testList "Forge command tests" [

testCase "Forge list command returns source files from project file" <| fun _ ->
let path, pf, f1, f2 = makeTempProject()
let output, errors, exitCode = Forge.list ForgePath pf
Directory.Delete(path, true)
Expect.equal output ["TestFile1.fs"; "TestFile2.fs"] "File names list matches"
Expect.equal errors [] "Errors are empty"
Expect.equal exitCode 0 "Exit code is zero"

testCase "Forge moves file down correctly" <| fun _ ->
let path, pf, f1, f2 = makeTempProject()
let output, errors, exitCode = Forge.moveFileDown ForgePath pf f1
let newOrder, _, _ = Forge.list ForgePath pf
Directory.Delete(path, true)
Expect.equal newOrder ["TestFile2.fs"; "TestFile1.fs"] "File order is correct"
Expect.equal errors [] "Errors are empty"
Expect.equal exitCode 0 "Exit code is zero"

testCase "Forge moves file up correctly" <| fun _ ->
let path, pf, f1, f2 = makeTempProject()
let output, errors, exitCode = Forge.moveFileUp ForgePath pf f2
let newOrder, _, _ = Forge.list ForgePath pf
Directory.Delete(path, true)
Expect.equal newOrder ["TestFile2.fs"; "TestFile1.fs"] "File order is correct"
Expect.equal errors [] "Errors are empty"
Expect.equal exitCode 0 "Exit code is zero"
]




8 changes: 3 additions & 5 deletions src/Mechanic.Tests/Mechanic.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Compile Include="Tests.fs" />
<Compile Include="Program.fs" />
<ProjectReference Include="..\Mechanic\Mechanic.fsproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Mechanic\Mechanic.fsproj" />
<Compile Include="Forge.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<Import Project="..\.paket\Paket.Restore.targets" />
</Project>
7 changes: 6 additions & 1 deletion src/Mechanic.Tests/Program.fs
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
module Program = let [<EntryPoint>] main _ = 0
module Program =

open Expecto

let [<EntryPoint>] main args =
runTestsInAssembly defaultConfig args
11 changes: 0 additions & 11 deletions src/Mechanic.Tests/Tests.fs

This file was deleted.

5 changes: 1 addition & 4 deletions src/Mechanic.Tests/paket.references
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
FsCheck.Xunit
Microsoft.NET.Test.Sdk
Unquote
xunit
xunit.runner.visualstudio
Expecto
55 changes: 55 additions & 0 deletions src/Mechanic/Forge.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module Mechanic.Forge

open System
open System.Diagnostics

let [<Literal>] defaultTimeout = 10000

let makeStartInfo path args =
ProcessStartInfo (
FileName = path,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
)

let setupHandlers (p:Process) =
let handler f _ (args:DataReceivedEventArgs) = f args.Data
let output = ResizeArray()
let errors = ResizeArray()
p.OutputDataReceived.AddHandler(DataReceivedEventHandler (handler output.Add))
p.ErrorDataReceived.AddHandler(DataReceivedEventHandler (handler errors.Add))
output, errors

let cleanOutput (output:ResizeArray<_>) =
output
|> Seq.filter (String.IsNullOrWhiteSpace >> not)
|> List.ofSeq

let execute (startInfo:ProcessStartInfo) =
use p = new Process(StartInfo = startInfo)
let output, errors = setupHandlers p
match p.Start() with
| true ->
p.BeginOutputReadLine()
p.BeginErrorReadLine()
p.WaitForExit(defaultTimeout) |> ignore
| false ->
failwith "Failed to start process"
cleanOutput output, cleanOutput errors, p.ExitCode

let moveFileUp (forgePath:string) (project:string) (file:string) =
sprintf "move file -p %s -n %s --up --no-prompt" project file
|> makeStartInfo forgePath
|> execute

let moveFileDown (forgePath:string) (project:string) (file:string) =
sprintf "move file -p %s -n %s --down --no-prompt" project file
|> makeStartInfo forgePath
|> execute

let list (forgePath:string) (project:string) =
sprintf "list files -p %s --no-prompt" project
|> makeStartInfo forgePath
|> execute
3 changes: 1 addition & 2 deletions src/Mechanic/Mechanic.fsproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageVersion>0.0.0</PackageVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="Forge.fs" />
<Compile Include="Library.fs" />
</ItemGroup>
<Import Project="..\.paket\Paket.Restore.targets" />
Expand Down
8 changes: 4 additions & 4 deletions src/paket.dependencies
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
source https://api.nuget.org/v3/index.json
storage:none

nuget FsCheck.Xunit
nuget Microsoft.NET.Test.Sdk
nuget Unquote
nuget xunit
nuget xunit.runner.visualstudio
nuget Expecto

git https://github.com/fsharp-editing/Forge.git master build:"build.cmd Build", OS: windows
git https://github.com/fsharp-editing/Forge.git master build:"build.sh Build", OS: mono

group Build
source https://api.nuget.org/v3/index.json
Expand Down
Loading