From 51991cab74ff9a9756fdea7c101935c0b73926da Mon Sep 17 00:00:00 2001 From: Smaug123 <3138005+Smaug123@users.noreply.github.com> Date: Thu, 17 Apr 2025 11:45:34 +0100 Subject: [PATCH] Fix another test --- ConsumePlugin/GeneratedArgs.fs | 29 +++++++-- .../TestArgParser/TestArgParser.fs | 2 +- WoofWare.Myriad.Plugins/ShibaGenerator.fs | 59 ++++++++++++++++++- 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/ConsumePlugin/GeneratedArgs.fs b/ConsumePlugin/GeneratedArgs.fs index 7182a1d..4606d99 100644 --- a/ConsumePlugin/GeneratedArgs.fs +++ b/ConsumePlugin/GeneratedArgs.fs @@ -2098,7 +2098,14 @@ module internal ArgParseHelpers_ConsumePlugin = Choice2Of2 ( "CONSUMEPLUGIN_THINGS" |> getEnvironmentVariable - |> (fun x -> System.Boolean.Parse x) + |> (fun x -> + if System.String.Equals (x, "1", System.StringComparison.OrdinalIgnoreCase) then + true + else if System.String.Equals (x, "0", System.StringComparison.OrdinalIgnoreCase) then + false + else + x |> (fun x -> System.Boolean.Parse x) + ) ) if errors.Count = 0 then @@ -2311,10 +2318,24 @@ module internal ArgParseHelpers_ConsumePlugin = "CONSUMEPLUGIN_THINGS" |> getEnvironmentVariable |> (fun x -> - if System.Boolean.Parse x = Consts.FALSE then - DryRunMode.Wet + if System.String.Equals (x, "1", System.StringComparison.OrdinalIgnoreCase) then + if true = Consts.FALSE then + DryRunMode.Wet + else + DryRunMode.Dry + else if System.String.Equals (x, "0", System.StringComparison.OrdinalIgnoreCase) then + if false = Consts.FALSE then + DryRunMode.Wet + else + DryRunMode.Dry else - DryRunMode.Dry + x + |> (fun x -> + if System.Boolean.Parse x = Consts.FALSE then + DryRunMode.Wet + else + DryRunMode.Dry + ) ) ) diff --git a/WoofWare.Myriad.Plugins.Test/TestArgParser/TestArgParser.fs b/WoofWare.Myriad.Plugins.Test/TestArgParser/TestArgParser.fs index 5b2b4d0..dde77cb 100644 --- a/WoofWare.Myriad.Plugins.Test/TestArgParser/TestArgParser.fs +++ b/WoofWare.Myriad.Plugins.Test/TestArgParser/TestArgParser.fs @@ -444,7 +444,7 @@ Required argument '--exact' received no value""" ] |> List.map TestCaseData - [] + [] let ``Bool env vars can be populated`` (envValue : string, boolValue : bool) = let getEnvVar (s : string) = s |> shouldEqual "CONSUMEPLUGIN_THINGS" diff --git a/WoofWare.Myriad.Plugins/ShibaGenerator.fs b/WoofWare.Myriad.Plugins/ShibaGenerator.fs index c59e677..b4842a7 100644 --- a/WoofWare.Myriad.Plugins/ShibaGenerator.fs +++ b/WoofWare.Myriad.Plugins/ShibaGenerator.fs @@ -139,6 +139,13 @@ module internal ShibaGenerator = ArgForm : SynExpr list /// Name of the field of the in-progress record storing this leaf. TargetConstructionField : Ident + /// If this is a boolean-like field (e.g. a bool or a flag DU), the help text should look a bit different: + /// we should lie to the user about the value of the cases there. + /// Similarly, if we're reading from an environment variable with the laxer parsing rules of accepting e.g. + /// "0" instead of "false", we need to know if we're reading a bool. + /// In that case, `boolCases` is Some, and contains the construction of the flag (or boolean, in which case + /// you get no data). + BoolCases : Choice option } /// A SynExpr of type `string` which we can display to the user at generated-program runtime to display all @@ -211,9 +218,16 @@ module internal ShibaGenerator = Positional = positional ArgForm = longForms TargetConstructionField = fieldName + BoolCases = None } |> ParseFunctionSpec.Leaf | PrimitiveType pt -> + let isBoolLike = + if pt |> List.map _.idText = [ "System" ; "Boolean" ] then + Some (Choice2Of2 ()) + else + identifyAsFlag flagDus ty |> Option.map Choice1Of2 + { ParseFn = SynExpr.createLambda @@ -226,6 +240,7 @@ module internal ShibaGenerator = Positional = positional ArgForm = longForms TargetConstructionField = fieldName + BoolCases = isBoolLike } |> ParseFunctionSpec.Leaf | Uri -> @@ -239,6 +254,7 @@ module internal ShibaGenerator = Positional = positional ArgForm = longForms TargetConstructionField = fieldName + BoolCases = None } |> ParseFunctionSpec.Leaf | TimeSpan -> @@ -301,6 +317,7 @@ module internal ShibaGenerator = Positional = positional ArgForm = longForms TargetConstructionField = fieldName + BoolCases = None } |> ParseFunctionSpec.Leaf | FileInfo -> @@ -316,6 +333,7 @@ module internal ShibaGenerator = Positional = positional ArgForm = longForms TargetConstructionField = fieldName + BoolCases = None } |> ParseFunctionSpec.Leaf | DirectoryInfo -> @@ -331,6 +349,7 @@ module internal ShibaGenerator = Positional = positional ArgForm = longForms TargetConstructionField = fieldName + BoolCases = None } |> ParseFunctionSpec.Leaf | OptionType eltTy -> @@ -501,6 +520,7 @@ module internal ShibaGenerator = Positional = positional ArgForm = longForms TargetConstructionField = fieldName + BoolCases = Some (Choice1Of2 flagDu) } |> ParseFunctionSpec.Leaf @@ -1020,6 +1040,43 @@ module internal ShibaGenerator = | _ -> failwith "unexpected: positional arguments should be a list" | None -> + let parseFn = + match leaf.BoolCases with + | Some boolLike -> + let trueCase, falseCase = + match boolLike with + | Choice2Of2 () -> SynExpr.CreateConst true, SynExpr.CreateConst false + | Choice1Of2 flag -> + FlagDu.FromBoolean flag (SynExpr.CreateConst true), + FlagDu.FromBoolean flag (SynExpr.CreateConst false) + + // We permit environment variables to be populated with 0 and 1 as well. + SynExpr.ifThenElse + (SynExpr.applyFunction + (SynExpr.createLongIdent [ "System" ; "String" ; "Equals" ]) + (SynExpr.tuple + [ + SynExpr.createIdent "x" + SynExpr.CreateConst "1" + SynExpr.createLongIdent + [ "System" ; "StringComparison" ; "OrdinalIgnoreCase" ] + ])) + (SynExpr.ifThenElse + (SynExpr.applyFunction + (SynExpr.createLongIdent [ "System" ; "String" ; "Equals" ]) + (SynExpr.tuple + [ + SynExpr.createIdent "x" + SynExpr.CreateConst "0" + SynExpr.createLongIdent + [ "System" ; "StringComparison" ; "OrdinalIgnoreCase" ] + ])) + (SynExpr.createIdent "x" |> SynExpr.pipeThroughFunction leaf.ParseFn) + falseCase) + trueCase + |> SynExpr.createLambda "x" + | None -> leaf.ParseFn + let extract = match leaf.Acc with | Accumulation.ChoicePositional choice -> failwith "TODO" @@ -1040,7 +1097,7 @@ module internal ShibaGenerator = |> SynExpr.pipeThroughFunction ( SynExpr.createIdent "getEnvironmentVariable" ) - |> SynExpr.pipeThroughFunction leaf.ParseFn + |> SynExpr.pipeThroughFunction parseFn | ArgumentDefaultSpec.FunctionCall name -> SynExpr.callMethod name.idText