|
| 1 | +namespace Grace.CLI.Tests |
| 2 | + |
| 3 | +open FsUnit |
| 4 | +open Grace.CLI |
| 5 | +open Grace.CLI.Command |
| 6 | +open Grace.CLI.Text |
| 7 | +open Grace.Shared.Client.Configuration |
| 8 | +open Grace.Types.Branch |
| 9 | +open Grace.Types.Reference |
| 10 | +open Grace.Types.Types |
| 11 | +open NUnit.Framework |
| 12 | +open Spectre.Console |
| 13 | +open System |
| 14 | +open System.IO |
| 15 | + |
| 16 | +[<NonParallelizable>] |
| 17 | +module ConnectTests = |
| 18 | + let private setAnsiConsoleOutput (writer: TextWriter) = |
| 19 | + let settings = AnsiConsoleSettings() |
| 20 | + settings.Out <- AnsiConsoleOutput(writer) |
| 21 | + AnsiConsole.Console <- AnsiConsole.Create(settings) |
| 22 | + let private runWithCapturedOutput (args: string array) = |
| 23 | + use writer = new StringWriter() |
| 24 | + let originalOut = Console.Out |
| 25 | + |
| 26 | + try |
| 27 | + Console.SetOut(writer) |
| 28 | + setAnsiConsoleOutput writer |
| 29 | + let exitCode = GraceCommand.main args |
| 30 | + exitCode, writer.ToString() |
| 31 | + finally |
| 32 | + Console.SetOut(originalOut) |
| 33 | + setAnsiConsoleOutput originalOut |
| 34 | + |
| 35 | + let private withTempDir (action: string -> unit) = |
| 36 | + let tempDir = Path.Combine(Path.GetTempPath(), $"grace-cli-tests-{Guid.NewGuid():N}") |
| 37 | + Directory.CreateDirectory(tempDir) |> ignore |
| 38 | + let originalDir = Environment.CurrentDirectory |
| 39 | + |
| 40 | + try |
| 41 | + Environment.CurrentDirectory <- tempDir |
| 42 | + action tempDir |
| 43 | + finally |
| 44 | + Environment.CurrentDirectory <- originalDir |
| 45 | + |
| 46 | + if Directory.Exists(tempDir) then |
| 47 | + try |
| 48 | + Directory.Delete(tempDir, true) |
| 49 | + with _ -> |
| 50 | + () |
| 51 | + |
| 52 | + let private getGraceConfigPath root = Path.Combine(root, ".grace", "graceconfig.json") |
| 53 | + |
| 54 | + [<Test>] |
| 55 | + let ``connect creates config when missing`` () = |
| 56 | + withTempDir (fun root -> |
| 57 | + let exitCode, _ = runWithCapturedOutput [| "connect" |] |
| 58 | + exitCode |> should equal -1 |
| 59 | + File.Exists(getGraceConfigPath root) |> should equal true) |
| 60 | + |
| 61 | + [<Test>] |
| 62 | + let ``connect retrieve default branch defaults to true`` () = |
| 63 | + let parseResult = GraceCommand.rootCommand.Parse([| "connect" |]) |
| 64 | + |
| 65 | + parseResult.GetValue<bool>(OptionName.RetrieveDefaultBranch) |
| 66 | + |> should equal true |
| 67 | + |
| 68 | + [<Test>] |
| 69 | + let ``connect retrieve default branch parses explicit false`` () = |
| 70 | + let parseResult = GraceCommand.rootCommand.Parse([| "connect"; OptionName.RetrieveDefaultBranch; "false" |]) |
| 71 | + |
| 72 | + parseResult.GetValue<bool>(OptionName.RetrieveDefaultBranch) |
| 73 | + |> should equal false |
| 74 | + |
| 75 | + [<Test>] |
| 76 | + let ``connect directory version selection precedence uses directory version id`` () = |
| 77 | + let directoryVersionId = Guid.NewGuid() |
| 78 | + let referenceId = Guid.NewGuid() |
| 79 | + |
| 80 | + let parseResult = |
| 81 | + GraceCommand.rootCommand.Parse( |
| 82 | + [| "connect" |
| 83 | + OptionName.DirectoryVersionId |
| 84 | + $"{directoryVersionId}" |
| 85 | + OptionName.ReferenceId |
| 86 | + $"{referenceId}" |
| 87 | + OptionName.ReferenceType |
| 88 | + "Commit" |] |
| 89 | + ) |
| 90 | + |
| 91 | + match Connect.getDirectoryVersionSelection parseResult with |
| 92 | + | Connect.UseDirectoryVersionId selected -> selected |> should equal directoryVersionId |
| 93 | + | other -> Assert.Fail($"Unexpected selection: {other}") |
| 94 | + |
| 95 | + [<Test>] |
| 96 | + let ``connect default directory version falls back to based-on`` () = |
| 97 | + let basedOnId = Guid.NewGuid() |
| 98 | + let branchDto = { BranchDto.Default with LatestPromotion = ReferenceDto.Default; BasedOn = { ReferenceDto.Default with DirectoryId = basedOnId } } |
| 99 | + |
| 100 | + Connect.resolveDefaultDirectoryVersionId branchDto |
| 101 | + |> should equal (Some basedOnId) |
| 102 | + |
| 103 | + [<Test>] |
| 104 | + let ``connect repository shortcut populates owner organization repository`` () = |
| 105 | + let parseResult = GraceCommand.rootCommand.Parse([| "connect"; "owner/org/repo" |]) |
| 106 | + let graceIds = GraceIds.Default |
| 107 | + |
| 108 | + match Connect.applyRepositoryShortcut parseResult graceIds with |
| 109 | + | Ok updated -> |
| 110 | + updated.OwnerName |> should equal "owner" |
| 111 | + updated.OrganizationName |> should equal "org" |
| 112 | + updated.RepositoryName |> should equal "repo" |
| 113 | + updated.OwnerId |> should equal Guid.Empty |
| 114 | + updated.OrganizationId |> should equal Guid.Empty |
| 115 | + updated.RepositoryId |> should equal Guid.Empty |
| 116 | + updated.HasOwner |> should equal true |
| 117 | + updated.HasOrganization |> should equal true |
| 118 | + updated.HasRepository |> should equal true |
| 119 | + | Error error -> Assert.Fail($"Unexpected error: {error.Error}") |
| 120 | + |
| 121 | + [<Test>] |
| 122 | + let ``connect repository shortcut rejects missing segments`` () = |
| 123 | + let parseResult = GraceCommand.rootCommand.Parse([| "connect"; "owner/repo" |]) |
| 124 | + let graceIds = GraceIds.Default |
| 125 | + |
| 126 | + match Connect.applyRepositoryShortcut parseResult graceIds with |
| 127 | + | Ok _ -> Assert.Fail("Expected error when repository shortcut is missing segments.") |
| 128 | + | Error error -> error.Error |> should contain "owner/organization/repository" |
| 129 | + |
| 130 | + [<Test>] |
| 131 | + let ``connect repository shortcut conflicts with explicit options`` () = |
| 132 | + let parseResult = GraceCommand.rootCommand.Parse([| "connect"; "owner/org/repo"; OptionName.OwnerName; "explicit-owner" |]) |
| 133 | + |
| 134 | + let graceIds = GraceIds.Default |
| 135 | + |
| 136 | + match Connect.applyRepositoryShortcut parseResult graceIds with |
| 137 | + | Ok _ -> Assert.Fail("Expected error when shortcut is combined with explicit options.") |
| 138 | + | Error error -> error.Error |> should contain "Provide either the repository shortcut" |
0 commit comments