Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 8ae90e5

Browse files
author
Patrick Thomson
committed
Quiet debug spew in specs.
This console barf makes reading though backscrolls unpleasant. Using Shelly rather than an abomination of a `system` call and adding a helper function to parse files quietly improved the situation greatly. This also contains changes to Util that make the file significantly easier to navigate, thanks to the power of `PartialTyepSignatures`. Fixes #140.
1 parent 23df12a commit 8ae90e5

File tree

10 files changed

+52
-150
lines changed

10 files changed

+52
-150
lines changed

semantic.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ common dependencies
6464
, scientific ^>= 0.3.6.2
6565
, safe-exceptions ^>= 0.1.7.0
6666
, semilattices ^>= 0.0.0.3
67+
, shelly >= 1.5 && <2
6768
, text ^>= 1.2.3.1
6869
, these >= 0.7 && <1
6970
, unix ^>= 2.7.2.2
@@ -307,7 +308,6 @@ library
307308
, reducers ^>= 3.12.3
308309
, semigroupoids ^>= 5.3.2
309310
, servant ^>= 0.15
310-
, shelly >= 1.5 && <2
311311
, split ^>= 0.2.3.3
312312
, stm-chans ^>= 3.0.0.4
313313
, template-haskell ^>= 2.14

src/Data/Blob.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Data.Blob
55
, Blob(..)
66
, Blobs(..)
77
, blobLanguage
8+
, NoLanguageForBlob (..)
89
, blobPath
910
, makeBlob
1011
, decodeBlobs

src/Semantic/Util.hs

Lines changed: 25 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
{-# LANGUAGE CPP, ConstraintKinds, Rank2Types, ScopedTypeVariables, TypeFamilies, TypeOperators #-}
2-
{-# OPTIONS_GHC -Wno-missing-signatures -O0 #-}
1+
{-# LANGUAGE CPP, ConstraintKinds, PartialTypeSignatures, Rank2Types, ScopedTypeVariables, TypeFamilies,
2+
TypeOperators #-}
3+
{-# OPTIONS_GHC -Wno-missing-signatures -Wno-partial-type-signatures -O0 #-}
34
module Semantic.Util
45
( evalGoProject
56
, evalPHPProject
@@ -10,6 +11,7 @@ module Semantic.Util
1011
, mergeErrors
1112
, reassociate
1213
, parseFile
14+
, parseFileQuiet
1315
) where
1416

1517
import Prelude hiding (readFile)
@@ -30,6 +32,7 @@ import Data.Blob.IO
3032
import Data.Graph (topologicalSort)
3133
import qualified Data.Language as Language
3234
import Data.List (uncons)
35+
import Data.Location
3336
import Data.Project hiding (readFile)
3437
import Data.Quieterm (Quieterm, quieterm)
3538
import Data.Sum (weaken)
@@ -47,70 +50,11 @@ import Semantic.Task
4750
import System.Exit (die)
4851
import System.FilePath.Posix (takeDirectory)
4952

50-
import Data.Location
51-
52-
-- The type signatures in these functions are pretty gnarly, but these functions
53-
-- are hit sufficiently often in the CLI and test suite so as to merit avoiding
54-
-- the overhead of repeated type inference. If you have to hack on these functions,
55-
-- it's recommended to remove all the type signatures and add them back when you
56-
-- are done (type holes in GHCi will help here).
57-
58-
justEvaluating :: Evaluator
59-
term
60-
Precise
61-
(Value term Precise)
62-
(ResumableC
63-
(BaseError (ValueError term Precise))
64-
(ResumableC
65-
(BaseError (AddressError Precise (Value term Precise)))
66-
(ResumableC
67-
(BaseError ResolutionError)
68-
(ResumableC
69-
(BaseError
70-
(EvalError term Precise (Value term Precise)))
71-
(ResumableC
72-
(BaseError (HeapError Precise))
73-
(ResumableC
74-
(BaseError (ScopeError Precise))
75-
(ResumableC
76-
(BaseError
77-
(UnspecializedError
78-
Precise (Value term Precise)))
79-
(ResumableC
80-
(BaseError
81-
(LoadError
82-
Precise
83-
(Value term Precise)))
84-
(FreshC
85-
(StateC
86-
(ScopeGraph
87-
Precise)
88-
(StateC
89-
(Heap
90-
Precise
91-
Precise
92-
(Value
93-
term
94-
Precise))
95-
(TraceByPrintingC
96-
(LiftC
97-
IO)))))))))))))
98-
result
99-
-> IO
100-
(Heap Precise Precise (Value term Precise),
101-
(ScopeGraph Precise,
102-
Either
103-
(SomeError
104-
(Sum
105-
'[BaseError (ValueError term Precise),
106-
BaseError (AddressError Precise (Value term Precise)),
107-
BaseError ResolutionError,
108-
BaseError (EvalError term Precise (Value term Precise)),
109-
BaseError (HeapError Precise),
110-
BaseError (ScopeError Precise),
111-
BaseError (UnspecializedError Precise (Value term Precise)),
112-
BaseError (LoadError Precise (Value term Precise))]))
113-
result))
53+
justEvaluating :: Evaluator term Precise (Value term Precise) _ result
54+
-> IO ( Heap Precise Precise (Value term Precise),
55+
( ScopeGraph Precise
56+
, Either (SomeError (Sum _)) result)
57+
)
11458
justEvaluating
11559
= runM
11660
. runEvaluator
@@ -128,75 +72,27 @@ justEvaluating
12872
. runAddressError
12973
. runValueError
13074

131-
type FileEvaluator syntax =
75+
type FileEvaluator err syntax =
13276
[FilePath]
13377
-> IO
134-
(Heap
135-
Precise
136-
Precise
137-
(Value
138-
(Quieterm (Sum syntax) Location) Precise),
139-
(ScopeGraph Precise,
140-
Either
141-
(SomeError
142-
(Sum
143-
'[BaseError
144-
(ValueError
145-
(Quieterm (Sum syntax) Location)
146-
Precise),
147-
BaseError
148-
(AddressError
149-
Precise
150-
(Value
151-
(Quieterm
152-
(Sum syntax) Location)
153-
Precise)),
154-
BaseError ResolutionError,
155-
BaseError
156-
(EvalError
157-
(Quieterm (Sum syntax) Location)
158-
Precise
159-
(Value
160-
(Quieterm
161-
(Sum syntax) Location)
162-
Precise)),
163-
BaseError (HeapError Precise),
164-
BaseError (ScopeError Precise),
165-
BaseError
166-
(UnspecializedError
167-
Precise
168-
(Value
169-
(Quieterm
170-
(Sum syntax) Location)
171-
Precise)),
172-
BaseError
173-
(LoadError
174-
Precise
175-
(Value
176-
(Quieterm
177-
(Sum syntax) Location)
178-
Precise))]))
179-
(ModuleTable
180-
(Module
181-
(ModuleResult
182-
Precise
183-
(Value
184-
(Quieterm (Sum syntax) Location)
185-
Precise))))))
78+
( Heap Precise Precise (Value (Quieterm (Sum syntax) Location) Precise),
79+
( ScopeGraph Precise
80+
, Either (SomeError (Sum err))
81+
(ModuleTable (Module (ModuleResult Precise (Value (Quieterm (Sum syntax) Location) Precise))))))
18682

187-
evalGoProject :: FileEvaluator Language.Go.Assignment.Syntax
83+
evalGoProject :: FileEvaluator _ Language.Go.Assignment.Syntax
18884
evalGoProject = justEvaluating <=< evaluateProject (Proxy :: Proxy 'Language.Go) goParser
18985

190-
evalRubyProject :: FileEvaluator Language.Ruby.Assignment.Syntax
86+
evalRubyProject :: FileEvaluator _ Language.Ruby.Assignment.Syntax
19187
evalRubyProject = justEvaluating <=< evaluateProject (Proxy @'Language.Ruby) rubyParser
19288

193-
evalPHPProject :: FileEvaluator Language.PHP.Assignment.Syntax
89+
evalPHPProject :: FileEvaluator _ Language.PHP.Assignment.Syntax
19490
evalPHPProject = justEvaluating <=< evaluateProject (Proxy :: Proxy 'Language.PHP) phpParser
19591

196-
evalPythonProject :: FileEvaluator Language.Python.Assignment.Syntax
197-
evalPythonProject = justEvaluating <=< evaluateProject (Proxy :: Proxy 'Language.Python) pythonParser
92+
evalPythonProject :: FileEvaluator _ Language.Python.Assignment.Syntax
93+
evalPythonProject = justEvaluating <=< evaluateProject (Proxy :: Proxy 'Language.Python) pythonParser
19894

199-
evalTypeScriptProject :: FileEvaluator Language.TypeScript.Assignment.Syntax
95+
evalTypeScriptProject :: FileEvaluator _ Language.TypeScript.Assignment.Syntax
20096
evalTypeScriptProject = justEvaluating <=< evaluateProject (Proxy :: Proxy 'Language.TypeScript) typescriptParser
20197

20298
evaluateProject proxy parser paths = withOptions debugOptions $ \ config logger statter ->
@@ -218,11 +114,13 @@ evaluateProject' session proxy parser paths = do
218114
(evaluate proxy (runDomainEffects (evalTerm withTermSpans)) modules)))))))
219115
either (die . displayException) pure res
220116

221-
parseFile :: Parser term -> FilePath -> IO term
117+
parseFile, parseFileQuiet :: Parser term -> FilePath -> IO term
222118
parseFile parser = runTask' . (parse parser <=< readBlob . fileForPath)
119+
parseFileQuiet parser = runTaskQuiet . (parse parser <=< readBlob . fileForPath)
223120

224-
runTask' :: TaskEff a -> IO a
121+
runTask', runTaskQuiet :: TaskEff a -> IO a
225122
runTask' task = runTaskWithOptions debugOptions task >>= either (die . displayException) pure
123+
runTaskQuiet task = runTaskWithOptions defaultOptions task >>= either (die . displayException) pure
226124

227125
mergeErrors :: Either (SomeError (Sum errs)) (Either (SomeError err) result) -> Either (SomeError (Sum (err ': errs))) result
228126
mergeErrors = either (\ (SomeError sum) -> Left (SomeError (weaken sum))) (either (\ (SomeError err) -> Left (SomeError (inject err))) Right)

test/Control/Abstract/Evaluator/Spec.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE TypeOperators #-}
2+
{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
23
module Control.Abstract.Evaluator.Spec
34
( spec
45
) where

test/Reprinting/Spec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ spec = describe "reprinting" $ do
2828
let path = "test/fixtures/javascript/reprinting/map.json"
2929
(src, tree) <- runIO $ do
3030
src <- blobSource <$> readBlobFromFile' (File path Language.JSON)
31-
tree <- parseFile jsonParser path
31+
tree <- parseFileQuiet jsonParser path
3232
pure (src, tree)
3333

3434
describe "tokenization" $ do

test/Rewriting/Go/Spec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ loopMatcher = target <* go where
3030
spec :: Spec
3131
spec = describe "recursively" $ do
3232
it "extracts integers" $ do
33-
parsed <- parseFile goParser "test/fixtures/go/matching/integers.go"
33+
parsed <- parseFileQuiet goParser "test/fixtures/go/matching/integers.go"
3434
let matched = recursively integerMatcher parsed
3535
sort matched `shouldBe` ["1", "2", "3"]
3636

3737
it "counts for loops" $ do
38-
parsed <- parseFile goParser "test/fixtures/go/matching/for.go"
38+
parsed <- parseFileQuiet goParser "test/fixtures/go/matching/for.go"
3939
let matched = recursively @[] @(Term _ _) loopMatcher parsed
4040
length matched `shouldBe` 2

test/Rewriting/JSON/Spec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ spec = describe "rewriting" $ do
4444
bytes <- runIO $ Source.fromUTF8 <$> B.readFile path
4545

4646
refactored <- runIO $ do
47-
json <- parseFile jsonParser path
47+
json <- parseFileQuiet jsonParser path
4848
let result = rewrite @Maybe (History.mark Unmodified json) (topDownAny onTrees)
4949
maybe (fail "rewrite failed") pure result
5050

test/Rewriting/Python/Spec.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ docstringMatcher =
2525
spec :: Spec
2626
spec = describe "matching/python" $ do
2727
it "matches top-level docstrings" $ do
28-
parsed <- parseFile pythonParser "test/fixtures/python/matching/docstrings.py"
28+
parsed <- parseFileQuiet pythonParser "test/fixtures/python/matching/docstrings.py"
2929
let matched = recursively @[] docstringMatcher parsed
3030
length matched `shouldBe` 2
3131

3232
it "matches docstrings recursively" $ do
33-
parsed <- parseFile pythonParser "test/fixtures/python/matching/docstrings_nested.py"
33+
parsed <- parseFileQuiet pythonParser "test/fixtures/python/matching/docstrings_nested.py"
3434
let matched = recursively @[] docstringMatcher parsed
3535
length matched `shouldBe` 3

test/Semantic/IO/Spec.hs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import Data.List
77
import System.Directory
88
import System.Exit (ExitCode (..))
99
import System.IO.Temp
10-
import System.Process
10+
import Data.String
1111

1212
import Data.Blob
1313
import Data.Handle
1414
import SpecHelpers hiding (readFile)
1515
import qualified Semantic.Git as Git
16-
16+
import Shelly (shelly, silently, cd, run_)
1717

1818
spec :: Spec
1919
spec = parallel $ do
@@ -22,16 +22,16 @@ spec = parallel $ do
2222
when hasGit . it "should read from a git directory" $ do
2323
-- This temporary directory will be cleaned after use.
2424
blobs <- liftIO . withSystemTempDirectory "semantic-temp-git-repo" $ \dir -> do
25-
let commands = [ "cd " <> dir
26-
, "git init"
27-
, "touch foo.py bar.rb"
28-
, "git add foo.py bar.rb"
29-
, "git config user.name 'Test'"
30-
, "git config user.email '[email protected]'"
31-
, "git commit -am 'test commit'"
32-
]
33-
exit <- system (intercalate " && " commands)
34-
when (exit /= ExitSuccess) (fail ("Couldn't run git properly in dir " <> dir))
25+
shelly $ silently $ do
26+
cd (fromString dir)
27+
let git = run_ "git"
28+
git ["init"]
29+
run_ "touch" ["foo.py", "bar.rb"]
30+
git ["add", "foo.py", "bar.rb"]
31+
git ["config", "user.name", "'Test'"]
32+
git ["config", "user.email", "'[email protected]'"]
33+
git ["commit", "-am", "'test commit'"]
34+
3535
readBlobsFromGitRepo (dir </> ".git") (Git.OID "HEAD") []
3636
let files = sortOn fileLanguage (blobFile <$> blobs)
3737
files `shouldBe` [ File "foo.py" Python

test/Semantic/Spec.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module Semantic.Spec (spec) where
22

3+
import Data.Either
4+
import SpecHelpers
5+
6+
import Data.Blob (NoLanguageForBlob (..))
37
import Semantic.Api hiding (Blob)
48
import Semantic.Git
5-
import System.Exit
6-
7-
import SpecHelpers
89

910
-- we need some lenses here, oof
1011
setBlobLanguage :: Language -> Blob -> Blob
@@ -18,7 +19,8 @@ spec = parallel $ do
1819
output `shouldBe` "{\"trees\":[{\"path\":\"methods.rb\",\"error\":\"NoLanguageForBlob \\\"methods.rb\\\"\",\"language\":\"Unknown\"}]}\n"
1920

2021
it "throws if given an unknown language for sexpression output" $ do
21-
runTaskOrDie (parseTermBuilder TermSExpression [setBlobLanguage Unknown methodsBlob]) `shouldThrow` (== ExitFailure 1)
22+
res <- runTaskWithOptions defaultOptions (parseTermBuilder TermSExpression [setBlobLanguage Unknown methodsBlob])
23+
void res `shouldBe` Left (NoLanguageForBlob "methods.rb")
2224

2325
it "renders with the specified renderer" $ do
2426
output <- fmap runBuilder . runTaskOrDie $ parseTermBuilder TermSExpression [methodsBlob]

0 commit comments

Comments
 (0)