-
-
Notifications
You must be signed in to change notification settings - Fork 441
Typed rules #4983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vidit-od
wants to merge
7
commits into
haskell:master
Choose a base branch
from
vidit-od:typed-rules
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Typed rules #4983
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d89ba1a
Add typed input paths for rule inputs
vidit-od 93b394f
Classify core rule input types
vidit-od 72b0085
Typed rules in Shakes
vidit-od 9070240
Typed rules in Ghcide
vidit-od c1eaf06
Other Typed rules
vidit-od 9014326
Typed rules tests
vidit-od d92ae10
Minor Changes
vidit-od File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| module TypedRuleTests (tests) where | ||
|
|
||
| import Config (testWithDummyPluginEmpty') | ||
| import Control.Monad (forM_) | ||
| import Control.Monad.IO.Class (liftIO) | ||
| import qualified Data.Text as T | ||
| import Development.IDE.Core.InputPath | ||
| import Development.IDE.Plugin.Test (ideResultSuccess) | ||
| import Development.IDE.Test (waitForAction, | ||
| waitForActionError) | ||
| import Development.IDE.Types.Location | ||
| import Language.LSP.Protocol.Types hiding | ||
| (SemanticTokenAbsolute (..), | ||
| SemanticTokenRelative (..), | ||
| SemanticTokensEdit (..), | ||
| mkRange) | ||
| import System.Directory (createDirectoryIfMissing) | ||
| import System.FilePath (takeDirectory, (</>)) | ||
| import Test.Tasty | ||
| import Test.Tasty.HUnit | ||
|
|
||
| tests :: TestTree | ||
| tests = testGroup "typed rules" | ||
| [ testGroup "InputPath classifiers" | ||
| [ testCase "dependency sources are not project Haskell inputs" $ do | ||
| let dep = toNormalizedFilePath' "/work/.hls/dependencies/base/Data/Maybe.hs" | ||
| toProjectHaskellInput dep @?= Nothing | ||
| unInputPath (toAllHaskellInput dep) @?= dep | ||
|
|
||
| , testCase "project Haskell inputs can be generalized to all Haskell inputs" $ do | ||
| let src = toNormalizedFilePath' "/work/src/Foo.hs" | ||
| case toProjectHaskellInput src of | ||
| Nothing -> assertFailure "Expected project source to classify" | ||
| Just input -> unInputPath (generalizeProjectInput input) @?= src | ||
|
|
||
| , testCase "specific file classifiers reject unrelated paths" $ do | ||
| let cabalFile = toNormalizedFilePath' "/work/pkg/pkg.cabal" | ||
| stackYaml = toNormalizedFilePath' "/work/pkg/stack.yaml" | ||
| source = toNormalizedFilePath' "/work/pkg/Foo.hs" | ||
| (unInputPath <$> toCabalFileInput cabalFile) @?= Just cabalFile | ||
| toCabalFileInput source @?= Nothing | ||
| (unInputPath <$> toStackYamlInput stackYaml) @?= Just stackYaml | ||
| toStackYamlInput source @?= Nothing | ||
|
|
||
| , testCase "bulk classifiers filter invalid paths" $ do | ||
| let projectFile = toNormalizedFilePath' "/work/src/Foo.hs" | ||
| depFile = toNormalizedFilePath' "/work/.hls/dependencies/pkg/Foo.hs" | ||
| cabalFile = toNormalizedFilePath' "/work/pkg/pkg.cabal" | ||
| stackYaml = toNormalizedFilePath' "/work/stack.yaml" | ||
| files = [projectFile, depFile, cabalFile, stackYaml] | ||
| nonDependencyFiles = [projectFile, cabalFile, stackYaml] | ||
| fmap unInputPath (classifyProjectHaskellInputs files) @?= | ||
| nonDependencyFiles | ||
| fmap unInputPath (classifyCabalFileInputs files) @?= [cabalFile] | ||
| fmap unInputPath (classifyStackYamlInputs files) @?= [stackYaml] | ||
|
|
||
| , testCase "dependency classifier does not match similar directory names" $ do | ||
| let dep = toNormalizedFilePath' "/work/.hls/dependencies/base/Data/Maybe.hs" | ||
| dep2 = toNormalizedFilePath' "/work/.hls/dependencies2/base/Data/Maybe.hs" | ||
| dep3 = toNormalizedFilePath' "/work/.hls/dependencies-extra/Foo.hs" | ||
| toProjectHaskellInput dep @?= Nothing | ||
| assertBool "dependencies2 should remain a project file" (toProjectHaskellInput dep2 /= Nothing) | ||
| assertBool "dependencies-extra should remain a project file" (toProjectHaskellInput dep3 /= Nothing) | ||
|
|
||
| , testCase "classifiers preserve ordering" $ do | ||
| let a = toNormalizedFilePath' "/work/src/A.hs" | ||
| b = toNormalizedFilePath' "/work/src/B.hs" | ||
| c = toNormalizedFilePath' "/work/src/C.hs" | ||
|
|
||
| fmap unInputPath (classifyProjectHaskellInputs [a,b,c]) @?= [a,b,c] | ||
|
|
||
| , testCase "project source remains project source after generalization round trip" $ do | ||
| let src = toNormalizedFilePath' "/work/src/Foo.hs" | ||
|
|
||
| case toProjectHaskellInput src of | ||
| Nothing -> assertFailure "Expected project source" | ||
| Just input -> unInputPath (generalizeProjectInput input) @?= src | ||
| ] | ||
|
|
||
| , testWithDummyPluginEmpty' "project-only rules reject dependency-source inputs" $ \dir -> do | ||
| let dependencyFile = dir </> ".hls" </> "dependencies" </> "pkg" </> "Data" </> "Maybe.hs" | ||
| dependencyDoc = TextDocumentIdentifier (filePathToUri dependencyFile) | ||
| projectOnlyRules = | ||
| [ "typecheck" | ||
| , "getLocatedImports" | ||
| , "getmodsummary" | ||
| , "getmodsummarywithouttimestamps" | ||
| , "getparsedmodule" | ||
| , "ghcsession" | ||
| , "ghcsessiondeps" | ||
| ] | ||
| liftIO $ createDirectoryIfMissing True (takeDirectory dependencyFile) | ||
| liftIO $ writeFile dependencyFile "module Data.Maybe where\n" | ||
|
|
||
| forM_ projectOnlyRules $ \rule -> do | ||
| err <- waitForActionError rule dependencyDoc | ||
| liftIO $ assertBool ("Unexpected error for " <> rule <> ": " <> T.unpack err) $ | ||
| "dependency file" `T.isInfixOf` err | ||
|
|
||
| -- Dependency source files still support all-Haskell/file-content rules. | ||
| fileContents <- waitForAction "getFileContents" dependencyDoc | ||
| liftIO $ assertBool "GetFileContents should accept dependency sources" $ | ||
| ideResultSuccess fileContents | ||
|
|
||
| , testWithDummyPluginEmpty' "all-haskell rules continue to accept dependency sources" $ \dir -> do | ||
| let dependencyFile = dir </> ".hls" </> "dependencies" </> "pkg" </> "Foo.hs" | ||
| dependencyDoc = TextDocumentIdentifier (filePathToUri dependencyFile) | ||
|
|
||
| liftIO $ createDirectoryIfMissing True (takeDirectory dependencyFile) | ||
| liftIO $ writeFile dependencyFile "module Foo where\nx = 1\n" | ||
| fileContents <- waitForAction "getFileContents" dependencyDoc | ||
| liftIO $ assertBool "GetFileContents should succeed" (ideResultSuccess fileContents) | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rules like filecontents, file existence, file modification time should probably work for all files, not just haskell files.