Skip to content

Commit d72cfdf

Browse files
committed
Added new test to ensure that both root and sub commands honor configuration changes.
1 parent 26d7b26 commit d72cfdf

File tree

3 files changed

+56
-32
lines changed

3 files changed

+56
-32
lines changed

src/TestConsole/Program.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let main _ =
1414
//ProgramSubCommand.main (args "list c:/data/")
1515
//ProgramSubCommand.main (args "delete c:/data/ --recursive")
1616
//ProgramTask.main (args "-w hello -w world")
17-
ProgramTokenReplacer.SCL.main (args "--package @shoelace-style/shoelace")
18-
//ProgramTokenReplacer.main (args "--package @shoelace-style/shoelace")
17+
//ProgramTokenReplacer.SCL.main (args "--package @shoelace-style/shoelace")
18+
ProgramTokenReplacer.main (args "get @shoelace-style/shoelace")
1919
//ProgramExtraInputs.main (args "-a A -b B -c C -d D -e E -1 1 -2 2 -3 3 -4 4 -5 5")
2020
//ProgramAppendWords.main (args "-w hello -w world -s \", \"")

src/TestConsole/ProgramTokenReplacer.fs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,24 @@ open FSharp.SystemCommandLine
2525
open Input
2626
open System.CommandLine.Parsing
2727

28-
let main argv =
29-
let app (package: string) =
30-
if package.StartsWith("@") then
31-
printfn $"{package}"
32-
0
33-
else
34-
eprintfn "The package name does not start with a leading @"
35-
1
28+
let getCmd =
29+
command "get" {
30+
description "Get a package by name"
31+
inputs (
32+
argument<string> "package"
33+
|> desc "A package with a leading @ name"
34+
)
35+
setAction (fun (package: string) ->
36+
if package.StartsWith("@") then
37+
printfn $"{package}"
38+
0 // success
39+
else
40+
eprintfn "The package name does not start with a leading @"
41+
1 // failure
42+
)
43+
}
3644

45+
let main argv =
3746
rootCommand argv {
3847
description "Can be called with a leading @ package"
3948
configure (fun cfg ->
@@ -43,24 +52,6 @@ let main argv =
4352
cfg.ResponseFileTokenReplacer <- new TryReplaceToken(fun _ _ _ -> true)
4453
cfg
4554
)
46-
inputs (
47-
// The package option needs to accept strings that start with "@" symbol.
48-
// For example, "--package @shoelace-style/shoelace".
49-
// To accomplish this, we will need to modify the default pipeline settings below.
50-
option<string> "--package"
51-
|> aliases ["-p"]
52-
|> desc "A package with a leading @ name"
53-
|> editOption (fun o ->
54-
o.CustomParser <- (fun res ->
55-
// Custom parser to allow leading @ in the package name
56-
let value = res.GetValueOrDefault<string>()
57-
if value.StartsWith("@")
58-
then value
59-
else
60-
res.AddError("oops")
61-
"---"
62-
)
63-
)
64-
)
65-
setAction app
55+
noAction
56+
addCommand getCmd
6657
}

src/Tests/SimpleAppTest.fs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ let ``05 empty array`` () =
9494

9595
/// In beta5, the action handler is never called if an input starts with "@", even if ResponseFileTokenReplacer is set to null.
9696
[<Test>]
97-
let ``06 Token Replacer`` () =
97+
let ``06 - rootCommand should use configuration`` () =
9898
testRootCommand "--package @shoelace-style/shoelace" {
9999
description "Can be called with a leading @ package"
100100
configure (fun cfg ->
@@ -114,9 +114,42 @@ let ``06 Token Replacer`` () =
114114
1 // failure
115115
)
116116
} =! 0
117+
118+
[<Test>]
119+
let ``07 - Child command should use configuration`` () =
120+
let getCmd =
121+
command "get" {
122+
description "Get a package by name"
123+
inputs (
124+
argument<string> "package"
125+
|> desc "A package with a leading @ name"
126+
)
127+
setAction (fun (package: string) ->
128+
handlerCalled <- true
129+
if package.StartsWith("@") then
130+
printfn $"{package}"
131+
0 // success
132+
else
133+
eprintfn "The package name does not start with a leading @"
134+
1 // failure
135+
)
136+
}
137+
138+
testRootCommand "get @shoelace-style/shoelace" {
139+
description "Can be called with a leading @ package"
140+
configure (fun cfg ->
141+
// Skip @ processing
142+
//cfg.UseTokenReplacer(fun _ _ _ -> false)
143+
//cfg.ResponseFileTokenReplacer <- null // in beta5, you must set ResponseFileTokenReplacer to null to skip @ processing
144+
cfg.ResponseFileTokenReplacer <- new TryReplaceToken(fun _ _ _ -> true)
145+
cfg
146+
)
147+
noAction
148+
addCommand getCmd
149+
} =! 0
117150

118151
[<Test>]
119-
let ``07 - Validators`` () =
152+
let ``08 - Validators`` () =
120153
handlerCalled <- true // Set to true to avoid false negatives in the test
121154
let args = args "-w delete -s *"
122155
let cfg =

0 commit comments

Comments
 (0)