Skip to content

test(cmd/morphic): pin the dash check past a separate value - #1516

Open
fuad-daoud wants to merge 12 commits into
mainfrom
test/cmd-dash-check-after-value
Open

fuad-daoud wants to merge 12 commits into
mainfrom
test/cmd-dash-check-after-value

Conversation

@fuad-daoud

@fuad-daoud fuad-daoud commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

In short: The CLI refuses a one-dash long flag such as -output, but no test put one after a flag with a separate value, where a regression would let it through and write a file named utput=ir.json. This adds fourteen rows, one after each spelling a flag ahead of it can take.

The one-dash flag check skips the value of a flag that takes one and carries on. If readAlone reported that case as refused, the scan would stop at the first such flag, and every one-dash flag after it would pass unchecked: --fail-on error -output=ir.json exited 0 and wrote a file named utput=ir.json. No row put a one-dash flag after a flag and its separate value.

This adds fourteen rows to TestRun_SingleDashLongFlagsAreRefused, one after each of these spellings of a flag ahead of the one-dash flag. A long flag has three forms: no value, a separate value and a joined value. A short flag has four: no value, a separate value, an attached value and a joined value. The last flag of a cluster of short flags takes the same four. After a separate value, a -- that the flag takes as its value is a row for a long flag, a short flag and a cluster. pflag reports a missing value from separate sites for a long flag and a short one, and reads the other spellings on their own paths. Taking a -- as a value is on the same branch as any other separate value, so its rows are there because a scan might look for -- itself, and not because of any pflag path.

Closes #829

Before and after

flowchart TB
  subgraph before ["Before"]
    b0["A later change makes the scan stop at a flag's separate value"]
    b1["--fail-on error -output=ir.json exits 0"]
    b2["A file named utput=ir.json is written, and the suite stays green"]
    b0 --> b1
    b1 --> b2
  end
  subgraph after ["After"]
    a0["A later change makes the scan stop at a flag's separate value"]
    a1["--fail-on error -output=ir.json exits 0"]
    a2["The new rows go red: they want the refusal and no file written"]
    a0 --> a1
    a1 --> a2
  end
  before ~~~ after
Loading

What it brings: A one-dash -output typed after another flag stays refused with its hint, rather than a later change turning it into a stray file.

Test plan

  • CI on this head, main at 2123d296 merged in, runs make gate.
  • Replacing return wantsValue with return refused in readAlone reddens the six rows whose value is separate (a long flag's, a short flag's, a cluster's, and the three -- values), each run alone; in a full run the file the first failing row leaves also reddens the rows after it, as they share a directory. The same plant under main's test file stays green, so the rows are what catch it.
  • Returning nil at case wantsValue also reddens those six rows, each run alone. i += 2 in place of i++ reddens them too, and TestRun_SingleDashArgumentsThatAreNotFlags reddens under it as well, also under main's test file.
  • Returning refused from that branch when !strings.HasPrefix(tok, "--") && len(tok) == 2, a shorthand on its own, reddens the two rows whose value follows a lone short flag (-o out.json and -o --), each run alone, and leaves the cluster's and the long flag's green.
  • Returning refused from that branch when !strings.HasPrefix(tok, "--") && len(tok) > 2 reddens the two cluster rows with a separate value (-ho out.json and -ho --), each run alone; the binary then exits 0 on -ho out.json -output=ir.json and prints help, where this tree exits 2.
  • Returning standsAlone from that branch for the same cluster condition reddens the cluster -- row alone, run alone; the binary then exits 0 on -ho -- -output=ir.json and prints help, and exits 2 on -ho -output=ir.json, where this tree exits 2 and 0 and prints help, respectively.
  • Cutting rest at its first -- before the scan reddens the three -- value rows, run alone; the binary then exits 0 on -o -- -output=ir.json and writes utput=ir.json, where this tree exits 2. Cutting rest at a -- only when the token before it is a long flag reddens the long flag's -- row alone, run alone, and --output -- -output=ir.json then writes utput=ir.json.
  • Stopping the scan at exactly --pretty, --fail-on=error, -oout.json or -o=out.json reddens that spelling's row, run alone, and leaves the other three green; under each such plant the row file before this change stays green and the binary writes utput=ir.json.
  • Stopping the scan at exactly -h reddens its row, run alone; the binary then exits 0, prints help and writes nothing, where this tree exits 2.
  • Stopping the scan at exactly -hoout.json, -ho=out.json or -hh reddens that cluster's row, run alone, and leaves the others green; stopping at any token that sets more than one flag reddens those three rows together. The binary then exits 0, prints help and writes nothing on each, where this tree exits 2.
  • Against the plant's binary, --fail-on error -output=ir.json exits 0 and writes utput=ir.json; on this tree it exits 2 and writes nothing.

The one-dash flag check skips the value of a flag that takes one and
carries on. If readAlone reported that case as refused, the scan would
stop at the first such flag and every one-dash flag after it would pass
unchecked: "--fail-on error -output=ir.json" exited 0 and wrote a file
named "utput=ir.json". No row put a one-dash flag after a flag and its
separate value, so the mutation left cmd/morphic green.

Add that row to TestRun_SingleDashLongFlagsAreRefused. It also asserts
that a refused run writes nothing to the working directory.
A shorthand's separate value takes the same branch of readAlone as a long
flag's, so the row holds both, and nothing would catch them coming apart.
The row's reader can now see that.
@fuad-daoud
fuad-daoud marked this pull request as draft November 5, 2026 21:50
@fuad-daoud
fuad-daoud removed the request for review from OmarAlJarrah November 5, 2026 21:50
pflag reports a flag's missing value from separate sites for a long flag and a shorthand, so one row cannot hold both: a change that made a shorthand's value end the scan left a stray file. Name the existing row for the long flag and add one for the short flag.
A one-dash long flag typed after a flag whose value is joined, attached or absent had no row, so a scan that stopped at any of those spellings wrote a stray file with the suite green. Add a row for a long flag with no value, a long flag's joined value, and a short flag's attached and joined values, beside the rows for a separate value.
A short flag that takes no value, such as -h, is read by pflag on its own path (the NoOptDefVal branch), which no row reached: a scan that stopped at -h stayed green while `morphic compile spec -h -output=ir.json` exited 0 and printed help instead of refusing the one-dash flag.
@fuad-daoud
fuad-daoud marked this pull request as ready for review November 5, 2026 23:40
@fuad-daoud
fuad-daoud marked this pull request as draft November 6, 2026 09:31
@fuad-daoud
fuad-daoud removed the request for review from OmarAlJarrah November 6, 2026 09:31
A token of several short flags, such as -ho, is read through a second turn of pflag's shorthand loop. A scan that stopped at any such token exited 0 and printed help instead of refusing the one-dash flag after it, with every row green.
A flag's separate value that is "--" is a value, not the terminator. A scan cut at the first "--" wrote a file named utput=ir.json for "-o -- -output=ir.json" and left every row green.
The rows covered a long flag's separate value, a short flag's four forms
and a cluster that ends in a separate value, but not the rest of the
grid, so a scan that stopped at a cluster, or read a cluster as complete,
or cut at a "--" that a long flag took as its value, left the suite
green.

Add a row for each remaining form: a cluster's attached value, its
joined value and a cluster that takes none, and a "--" taken as a value
by a long flag and by a cluster. Rename the existing "--" row to name
its short flag.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cli: no test pins that the one-dash flag check reads past a flag's value

1 participant