Skip to content

Commit 95de174

Browse files
committed
Handle hindent freaking out on bad input in a more useful way
1 parent f2bcc55 commit 95de174

File tree

4 files changed

+53
-75
lines changed

4 files changed

+53
-75
lines changed

extra/Lamdera.hs

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ import qualified Data.Text.IO
150150
import qualified Data.Char as Char
151151
import qualified Data.List as List
152152

153-
import System.Exit (exitFailure)
153+
import qualified System.Exit (ExitCode(..))
154154

155155
import Prelude hiding (lookup)
156156
import qualified Data.ByteString as BS
@@ -510,67 +510,21 @@ formatHaskellValue label v =
510510

511511
hindentPrintLabelled :: Show a => Text -> a -> IO a
512512
hindentPrintLabelled label v = do
513-
let
514-
input = Text.Show.Unicode.ushow v
515-
516-
-- if Prelude.length input > 10000
517-
-- then
518-
-- atomicPutStrLn $ "❌SKIPPED display, value show > 10,000 chars, here's a clip:\n" <> Prelude.take 1000 input
519-
-- else do
520-
521-
pathM <- Dir.findExecutable "hindent"
522-
case pathM of
523-
Just _ -> do
524-
(exit, stdout, stderr) <- System.Process.readProcessWithExitCode "hindent" ["--line-length","150"] input
525-
if Prelude.length stderr > 0
526-
then
527-
atomicPutStrLn $
528-
"\n🔶 "
529-
<> T.unpack label
530-
<> "\n->"
531-
<> stderr
532-
<> "\n📥 for input: \n"
533-
<> input
534-
535-
else
536-
atomicPutStrLn $
537-
"\n🔶 "
538-
<> T.unpack label
539-
<> "\n->"
540-
<> stdout
541-
Nothing -> do
542-
debug_ "hindent missing, formatting without"
543-
atomicPutStrLn $ show v
544-
513+
let input = Text.Show.Unicode.ushow v
514+
formatted <- hindent_ input
515+
atomicPutStrLn $
516+
"\n🔶 "
517+
<> T.unpack label
518+
<> "\n->"
519+
<> T.unpack formatted
545520
pure v
546521

547522

548523
hindentPrint :: Show a => a -> IO a
549524
hindentPrint v = do
550-
let
551-
input = Text.Show.Unicode.ushow v
552-
553-
pathM <- Dir.findExecutable "hindent"
554-
case pathM of
555-
Just _ -> do
556-
(exit, stdout, stderr) <- System.Process.readProcessWithExitCode "hindent" ["--line-length","150"] input
557-
if Prelude.length stderr > 0
558-
then
559-
atomicPutStrLn $
560-
"\nerrors: \n"
561-
<> stderr
562-
<> "\n📥 for input: \n"
563-
<> input
564-
<> "\nwith output: \n"
565-
<> stdout
566-
567-
else
568-
atomicPutStrLn stdout
569-
570-
Nothing -> do
571-
debug_ "hindent missing, formatting without"
572-
atomicPutStrLn $ show v
573-
525+
let input = Text.Show.Unicode.ushow v
526+
formatted <- hindent_ input
527+
Ext.Common.atomicPutStrLn_ formatted
574528
pure v
575529

576530

@@ -581,15 +535,33 @@ hindent v =
581535

582536
hindent_ :: String -> IO Text
583537
hindent_ s = do
584-
-- pure $ T.pack s
585-
(exit, stdout, stderr) <-
586-
System.Process.readProcessWithExitCode "hindent" ["--line-length","150"] s
587-
`catchError` (\err -> pure (error "no exit code on failure", s, "hindent failed"))
588-
if Prelude.length stderr > 0
589-
then
590-
pure $ T.pack stderr
591-
else
592-
pure $ T.pack stdout
538+
pathM <- Dir.findExecutable "hindent"
539+
case pathM of
540+
Just _ -> do
541+
(exit, stdout, stderr) <-
542+
System.Process.readProcessWithExitCode "hindent" ["--line-length","150"] s
543+
`catchError` (\err -> pure (error "no exit code on failure", s, "hindent failed on input"))
544+
545+
if exit /= System.Exit.ExitSuccess
546+
then
547+
pure $ T.pack stderr
548+
else
549+
if Prelude.length stderr > 0
550+
then
551+
pure $ T.pack $
552+
"\nerrors: \n"
553+
<> stderr
554+
<> "\n📥 for input: \n"
555+
<> s
556+
<> "\nwith output: \n"
557+
<> stdout
558+
559+
else
560+
pure $ T.pack stdout
561+
562+
Nothing -> do
563+
debug_ "🔥 hindent missing, returning without formatting"
564+
pure $ T.pack s
593565

594566

595567
hindentFormatValue :: Show a => a -> Text

extra/readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ The test framework is a very lightweight vendored copy of `EasyTest.hs`, defined
3333
The easiest way to run is from the project root:
3434

3535
```
36-
$ stack ghci
36+
$ TOKEN="..." LDEBUG=1 LOVR=~/lamdera/overrides stack ghci
3737
λ: Test.all
38+
...
39+
✅ 38 tests passed, no failures!
3840
```
3941

4042
### Developing

test/Test.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ For more information on how to use the GHCi debugger, see the GHC User's Guide.
9898
-- Current target for ghci :rr command. See ~/.ghci config file, which should contain
9999
-- something like `:def rr const $ return $ unlines [":r","Test.target"]`
100100

101-
-- target = Test.all
101+
target = Test.all
102102
-- target = checkProject
103103

104104

@@ -128,7 +128,7 @@ previewProject = do
128128

129129
-- target = buildTestHarnessToProductionJs
130130
-- target = checkProjectCompiles
131-
target = previewProject
131+
-- target = previewProject
132132
-- target = liveReloadLive
133133
-- target = Test.Wire.all
134134
-- target = checkUserConfig
@@ -139,6 +139,7 @@ target = previewProject
139139
-- target = Lamdera.ReverseProxy.start
140140
-- target = Test.Check.mockBuildSh "/Users/mario/lamdera-deploys/test-local-v1" "test-local"
141141
-- target = Test.Check.mockBuildSh "/Users/mario/dev/test/lamdera-init" "test-local"
142+
-- target = Test.Caching.all
142143

143144
-- target = do
144145
-- let p = "/Users/mario/dev/projects/elm-pages/examples/end-to-end"
@@ -220,16 +221,16 @@ buildTestHarnessToProductionJs = do
220221
liveReloadLive = do
221222

222223
-- setEnv "LOVR" "/Users/mario/dev/projects/lamdera/overrides"
223-
-- setEnv "LDEBUG" "1"
224-
-- setEnv "EXPERIMENTAL" "1"
224+
setEnv "LDEBUG" "1"
225+
setEnv "EXPERIMENTAL" "1"
225226

226227
-- let p = "/Users/mario/lamdera/test/v1"
227228
-- let p = "/Users/mario/dev/projects/bento-life"
228-
-- let p = "/Users/mario/dev/projects/lamdera-dashboard"
229+
let p = "/Users/mario/dev/projects/lamdera-dashboard"
229230

230231
-- let p = "/Users/mario/dev/test/lamdera-init"
231232
-- let p = "/Users/mario/dev/test/nu-ashworld-lamdera"
232-
let p = "/Users/mario/dev/test/town-collab"
233+
-- let p = "/Users/mario/dev/test/town-collab"
233234

234235
-- let p = "/Users/mario/dev/projects/otstats"
235236
-- let p = "/Users/mario/work/codespecs"

test/Test/Helpers.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ catchOutput :: IO () -> EasyTest.Test Text
7676
catchOutput action = do
7777
-- https://hackage.haskell.org/package/main-tester-0.2.0.1/docs/Test-Main.html
7878
pr <- EasyTest.io $ captureProcessResult action
79-
EasyTest.io $ hindent pr
79+
-- EasyTest.io $ hindent pr
80+
pure $ T.pack $ show pr
8081

8182

8283
catchOutputStdErr :: IO () -> EasyTest.Test Text
@@ -89,4 +90,6 @@ catchOutputStdErr action = do
8990

9091
captureProcessOutput action = do
9192
pr <- captureProcessResult action
92-
hindent pr
93+
-- hindent pr
94+
pure $ T.pack $ show pr
95+

0 commit comments

Comments
 (0)