Skip to content

Commit fbfb548

Browse files
authored
Fix detection of some flags passed to purs (#1286)
1 parent b7c0483 commit fbfb548

File tree

4 files changed

+57
-32
lines changed

4 files changed

+57
-32
lines changed

src/Spago/Cmd.purs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ getStderr = either _.stderr _.stderr
130130
-- | For example, trying to find the `output` arg
131131
-- | we would run
132132
-- | `findFlags { flags: [ "-o", "--output" ], args }`
133-
-- | where `args` is one of the 5 variants below:
133+
-- | where `args` is one of the 6 variants below:
134+
-- | - args are just one element:
135+
-- | - `[ "--output"]`
134136
-- | - args are two separate array elements:
135137
-- | - `[ "-o", "dir"]`
136138
-- | - `[ "--output", "dir"]`
@@ -139,27 +141,31 @@ getStderr = either _.stderr _.stderr
139141
-- | - `[ "--output dir"]`
140142
-- | - `[ "--output=dir"]`
141143
findFlag :: { flags :: Array String, args :: Array String } -> Maybe String
142-
findFlag { flags, args } = if len == 0 then Nothing else go 0
144+
findFlag { flags, args } = if argsLen == 0 then Nothing else go 0
143145
where
144-
len = Array.length args
145-
lastIdx = len - 1
146-
go idx = do
147-
let arg = unsafePartial $ Array.unsafeIndex args idx
148-
case Array.findMap (\flag -> stripFlag flag arg) flags of
149-
Just (Tuple isOneCharFlag restOfArg)
150-
| restOfArg == "" -> do
151-
Array.index args $ idx + 1
152-
| otherwise ->
153-
dropExtra isOneCharFlag restOfArg
154-
Nothing
155-
| idx < lastIdx ->
156-
go $ idx + 1
157-
| otherwise ->
158-
Nothing
146+
argsLen = Array.length args
147+
lastArgIdx = argsLen - 1
159148

149+
go idx = do
150+
let currentArg = unsafePartial $ Array.unsafeIndex args idx
151+
case Array.findMap (\flag -> stripFlag flag currentArg) flags of
152+
Just (Tuple isOneCharFlag restOfArg) -> case restOfArg of
153+
-- If the flag is the entirety of the arg, we need to look at the next arg:
154+
"" -> case Array.index args (idx + 1) of
155+
-- If there's a next arg we return it
156+
Just next -> Just next
157+
-- If there's not then this was a boolean flag
158+
Nothing -> Just ""
159+
_ -> dropExtra isOneCharFlag restOfArg
160+
Nothing -> case idx < lastArgIdx of
161+
true -> go (idx + 1)
162+
false -> Nothing
163+
164+
stripFlag :: String -> String -> Maybe (Tuple Boolean String)
160165
stripFlag flag arg =
161166
Tuple (isSingleCharFlag flag) <$> String.stripPrefix (Pattern flag) arg
162167

168+
dropExtra :: Boolean -> String -> Maybe String
163169
dropExtra isOneCharFlag restOfArg =
164170
if isOneCharFlag then dropSpace restOfArg
165171
else dropSpace restOfArg <|> dropEquals restOfArg

test-fixtures/json-errors-err.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Reading Spago workspace configuration...
2+
3+
✓ Selecting package to build: aaa
4+
5+
Downloading dependencies...
6+
No lockfile found, generating it...
7+
Lockfile written to spago.lock. Please commit this file.
8+
Building...
9+
10+
✘ Can't pass `--json-errors` option directly to purs.
11+
Use the --json-errors flag for Spago.

test/Spago/Build.purs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ spec = Spec.around withTempDir do
3434

3535
Spec.it "passes options to purs" \{ spago } -> do
3636
spago [ "init" ] >>= shouldBeSuccess
37-
spago [ "build", "--purs-args", "--verbose-errors", "--purs-args", "--json-errors" ] >>= shouldBeSuccess
37+
spago [ "build", "--purs-args", "--verbose-errors", "--purs-args", "--comments" ] >>= shouldBeSuccess
38+
39+
Spec.it "can't pass the --json-errors flag to purs" \{ spago, fixture } -> do
40+
spago [ "init", "--name", "aaa" ] >>= shouldBeSuccess
41+
spago [ "build", "--purs-args", "--json-errors" ] >>= shouldBeFailureErr (fixture "json-errors-err.txt")
3842

3943
Spec.it "can use a different output folder" \{ spago } -> do
4044
spago [ "init" ] >>= shouldBeSuccess
@@ -224,8 +228,8 @@ spec = Spec.around withTempDir do
224228
, result
225229
, sanitize:
226230
String.trim
227-
>>> String.replaceAll (String.Pattern $ "src\\") (String.Replacement "src/")
228-
>>> String.replaceAll (String.Pattern $ "\r\n") (String.Replacement "\n")
231+
>>> String.replaceAll (String.Pattern $ "src\\") (String.Replacement "src/")
232+
>>> String.replaceAll (String.Pattern $ "\r\n") (String.Replacement "\n")
229233
}
230234

231235
FS.copyTree { src: fixture "build/1148-warnings-diff-errors", dst: "." }

test/Spago/Unit/FindFlags.purs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,36 @@ module Test.Spago.Unit.FindFlags where
22

33
import Prelude
44

5-
import Data.Maybe (fromMaybe)
5+
import Data.Maybe (Maybe(..))
66
import Spago.Cmd as Cmd
7-
import Test.Spec as Spec
87
import Test.Spec (Spec)
8+
import Test.Spec as Spec
99
import Test.Spec.Assertions as Assertions
1010

1111
spec :: Spec Unit
1212
spec = do
1313
Spec.describe "findFlags" $ do
1414
Spec.it "[\"-o\", \"something\"]" $ do
15-
let a = fromMaybe "" $ Cmd.findFlag { flags: [ "-o", "--output" ], args: [ "-o", "something" ] }
16-
let b = "something"
15+
let a = Cmd.findFlag { flags: [ "-o", "--output" ], args: [ "-o", "something" ] }
16+
let b = Just "something"
1717
a `Assertions.shouldEqual` b
1818
Spec.it "[\"--output\", \"something\"]" $ do
19-
let a = fromMaybe "" $ Cmd.findFlag { flags: [ "-o", "--output" ], args: [ "--output", "something" ] }
20-
let b = "something"
19+
let a = Cmd.findFlag { flags: [ "-o", "--output" ], args: [ "--output", "something" ] }
20+
let b = Just "something"
2121
a `Assertions.shouldEqual` b
2222
Spec.it "[\"-o something\"]" $ do
23-
let a = fromMaybe "" $ Cmd.findFlag { flags: [ "-o", "--output" ], args: [ "-o something" ] }
24-
let b = "something"
23+
let a = Cmd.findFlag { flags: [ "-o", "--output" ], args: [ "-o something" ] }
24+
let b = Just "something"
2525
a `Assertions.shouldEqual` b
2626
Spec.it "[\"--output something\"]" $ do
27-
let a = fromMaybe "" $ Cmd.findFlag { flags: [ "-o", "--output" ], args: [ "--output something" ] }
28-
let b = "something"
27+
let a = Cmd.findFlag { flags: [ "-o", "--output" ], args: [ "--output something" ] }
28+
let b = Just "something"
2929
a `Assertions.shouldEqual` b
3030
Spec.it "[\"--output=something\"]" $ do
31-
let a = fromMaybe "" $ Cmd.findFlag { flags: [ "-o", "--output" ], args: [ "--output=something" ] }
32-
let b = "something"
31+
let a = Cmd.findFlag { flags: [ "-o", "--output" ], args: [ "--output=something" ] }
32+
let b = Just "something"
33+
a `Assertions.shouldEqual` b
34+
Spec.it "[ '--verbose-errors', '--json-errors' ]" do
35+
let a = Cmd.findFlag { flags: [ "--json-errors" ], args: [ "--verbose-errors", "--json-errors" ] }
36+
let b = Just ""
3337
a `Assertions.shouldEqual` b

0 commit comments

Comments
 (0)