-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathHelpDoesNotReadConfig.Tests.fs
More file actions
115 lines (98 loc) · 4.25 KB
/
HelpDoesNotReadConfig.Tests.fs
File metadata and controls
115 lines (98 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
namespace Grace.CLI.Tests
open FsUnit
open Grace.CLI
open Grace.Shared.Client.Configuration
open Grace.Shared.Utilities
open NUnit.Framework
open System
open System.IO
[<NonParallelizable>]
module HelpDoesNotReadConfigTests =
let private withTempDir (action: string -> unit) =
let tempDir = Path.Combine(Path.GetTempPath(), $"grace-cli-tests-{Guid.NewGuid():N}")
Directory.CreateDirectory(tempDir) |> ignore
let originalDir = Environment.CurrentDirectory
try
Environment.CurrentDirectory <- tempDir
action tempDir
finally
Environment.CurrentDirectory <- originalDir
if Directory.Exists(tempDir) then
try
Directory.Delete(tempDir, true)
with _ ->
()
let private writeInvalidConfig (root: string) =
let graceDir = Path.Combine(root, ".grace")
Directory.CreateDirectory(graceDir) |> ignore
File.WriteAllText(Path.Combine(graceDir, "graceconfig.json"), "not json")
let private writeValidConfig (root: string) (ownerId: Guid) (orgId: Guid) (repoId: Guid) (branchId: Guid) =
let graceDir = Path.Combine(root, ".grace")
Directory.CreateDirectory(graceDir) |> ignore
let config = GraceConfiguration()
config.OwnerId <- ownerId
config.OrganizationId <- orgId
config.RepositoryId <- repoId
config.BranchId <- branchId
let json = serialize config
File.WriteAllText(Path.Combine(graceDir, "graceconfig.json"), json)
[<Test>]
let ``help works with invalid config`` () =
withTempDir (fun root ->
writeInvalidConfig root
let exitCode = GraceCommand.main [| "access"; "grant-role"; "-h" |]
exitCode |> should equal 0)
[<Test>]
let ``help works without config`` () =
withTempDir (fun _ ->
let exitCode = GraceCommand.main [| "access"; "grant-role"; "-h" |]
exitCode |> should equal 0)
[<Test>]
let ``help shows symbolic defaults`` () =
withTempDir (fun _ ->
use writer = new StringWriter()
let originalOut = Console.Out
try
Console.SetOut(writer)
let exitCode = GraceCommand.main [| "access"; "grant-role"; "-h" |]
exitCode |> should equal 0
finally
Console.SetOut(originalOut)
let output = writer.ToString()
output |> should contain "[default: current OwnerId]"
output |> should contain "[default: current OrganizationId]"
output |> should contain "[default: current RepositoryId]"
output |> should contain "[default: current BranchId]"
output |> should contain "[default: new NanoId]"
)
[<Test>]
let ``create help rewrites empty guid defaults`` () =
withTempDir (fun _ ->
use writer = new StringWriter()
let originalOut = Console.Out
try
Console.SetOut(writer)
let exitCode = GraceCommand.main [| "repository"; "create"; "-h" |]
exitCode |> should equal 0
finally
Console.SetOut(originalOut)
let output = writer.ToString()
output |> should contain "[default: current OwnerId]"
output |> should contain "[default: current OrganizationId]"
output |> should contain "[default: new Guid]"
output |> should not' (contain "00000000-0000-0000-0000-000000000000")
)
[<Test>]
let ``getNormalizedIdsAndNames falls back to config ids`` () =
withTempDir (fun root ->
let ownerId = Guid.NewGuid()
let orgId = Guid.NewGuid()
let repoId = Guid.NewGuid()
let branchId = Guid.NewGuid()
writeValidConfig root ownerId orgId repoId branchId
let parseResult = GraceCommand.rootCommand.Parse([| "access"; "grant-role" |])
let graceIds = Services.getNormalizedIdsAndNames parseResult
graceIds.OwnerId |> should equal ownerId
graceIds.OrganizationId |> should equal orgId
graceIds.RepositoryId |> should equal repoId
graceIds.BranchId |> should equal branchId)