Skip to content

Commit 82c1535

Browse files
authored
Fix Change Type Signature Plugin test suite for 9.2.1 (#2761)
* Add change-type-signature test suite and enable alternate-number-format for 9.2 * Revert 9.2.1 enable tests for alternate-number-format * Clean up test file * GHC 9.2.1 test fixes * -Wall fixes * Add to fix test failures * Bump Version
1 parent d735c37 commit 82c1535

14 files changed

+98
-46
lines changed

.github/workflows/test.yml

+4
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ jobs:
236236
name: Test hls-selection-range-plugin test suite
237237
run: cabal test hls-selection-range-plugin --test-options="$TEST_OPTS" || cabal test hls-selection-range-plugin --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test hls-selection-range-plugin --test-options="$TEST_OPTS"
238238

239+
- if: matrix.test
240+
name: Test hls-change-type-signature test suite
241+
run: cabal test hls-change-type-signature-plugin --test-options="$TEST_OPTS" || cabal test hls-change-type-signature-plugin --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test hls-change-type-signature-plugin --test-options="$TEST_OPTS"
242+
239243
test_post_job:
240244
if: always()
241245
runs-on: ubuntu-latest

plugins/hls-change-type-signature-plugin/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ If the plugin receives enough information it can correctly change the signature.
1414
## Changelog
1515
### 1.0.0.0
1616
- First Release
17+
18+
### 1.0.1.0
19+
- Fix 9.2 Test failures (`waitForProgressDone`)
20+
- Add extra test scenarios for error message diffs in 9.2
21+
- Remove regex parsing for simple `Text` manipulation

plugins/hls-change-type-signature-plugin/hls-change-type-signature-plugin.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.4
22
name: hls-change-type-signature-plugin
3-
version: 1.0.0.0
3+
version: 1.0.1.0
44
synopsis: Change a declarations type signature with a Code Action
55
description:
66
Please see the README on GitHub at <https://github.com/haskell/plugins/hls-change-type-signature-plugin/README.md>

plugins/hls-change-type-signature-plugin/src/Ide/Plugin/ChangeTypeSignature.hs

+9-14
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ data ChangeSignature = ChangeSignature {
6666
, declSrcSpan :: RealSrcSpan
6767
-- | the diagnostic to solve
6868
, diagnostic :: Diagnostic
69-
}
69+
}
7070

7171
-- | Constraint needed to trackdown OccNames in signatures
7272
type SigName = (HasOccName (IdP GhcPs))
@@ -99,6 +99,8 @@ errorMessageRegexes :: [Text]
9999
errorMessageRegexes = [ -- be sure to add new Error Messages Regexes at the bottom to not fail any existing tests
100100
"Expected type: (.+)\n +Actual type: (.+)\n(.|\n)+In an equation for ‘(.+)’"
101101
, "Couldn't match expected type ‘(.+)’ with actual type ‘(.+)’\n(.|\n)+In an equation for ‘(.+)’"
102+
-- GHC >9.2 version of the first error regex
103+
, "Expected: (.+)\n +Actual: (.+)\n(.|\n)+In an equation for ‘(.+)’"
102104
]
103105

104106
-- | Given a String with the name of a declaration, GHC's "Expected Type", find the declaration that matches
@@ -129,25 +131,18 @@ findSigLocOfStringDecl decls expectedType declName = something (const Nothing `e
129131
compareId (L _ id') = declName == occNameString (occName id')
130132

131133

132-
133134
-- | Pretty Print the Type Signature (to validate GHC Error Message)
134135
sigToText :: Sig GhcPs -> Maybe Text
135136
sigToText = \case
136-
ts@TypeSig {} -> stripSignature $ T.pack $ prettyPrint ts
137+
ts@TypeSig {} -> Just $ stripSignature $ T.pack $ prettyPrint ts
137138
_ -> Nothing
138139

139-
stripSignature :: Text -> Maybe Text
140+
stripSignature :: Text -> Text
140141
-- for whatever reason incoming signatures MAY have new lines after "::" or "=>"
141-
stripSignature sig = case T.filter (/= '\n') sig =~ sigRegex :: (Text, Text, Text, [Text]) of
142-
-- No constraints (Monad m =>)
143-
(_, _, _, [sig']) -> Just $ T.strip sig'
144-
-- Ignore constraints (Monad m =>)
145-
(_, _, _, [_, sig']) -> Just $ T.strip sig'
146-
_ -> Nothing
147-
where
148-
-- we want to test everthing after the constraints (GHC never gives us the constraint in the expected signature)
149-
sigRegex = ".* :: (.*=>)?(.*)" :: Text
150-
142+
stripSignature (T.filter (/= '\n') -> sig) = if T.isInfixOf " => " sig
143+
-- remove constraints
144+
then T.strip $ snd $ T.breakOnEnd " => " sig
145+
else T.strip $ snd $ T.breakOnEnd " :: " sig
151146

152147
changeSigToCodeAction :: Uri -> ChangeSignature -> Command |? CodeAction
153148
changeSigToCodeAction uri ChangeSignature{..} = InR CodeAction { _title = mkChangeSigTitle declName actualType

plugins/hls-change-type-signature-plugin/test/Main.hs

+30-21
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
module Main where
22

3+
import Control.Monad (void)
34
import Data.Either (rights)
45
import Data.Text (Text)
56
import qualified Data.Text as T
67
import qualified Data.Text.IO as TIO
78
import Ide.Plugin.ChangeTypeSignature (errorMessageRegexes)
89
import qualified Ide.Plugin.ChangeTypeSignature as ChangeTypeSignature
910
import System.FilePath ((<.>), (</>))
10-
import Test.Hls (CodeAction (..),
11-
CodeActionKind (CodeActionQuickFix),
12-
Command, IdeState,
11+
import Test.Hls (CodeAction (..), Command,
12+
GhcVersion (GHC92), IdeState,
1313
PluginDescriptor,
1414
Position (Position),
1515
Range (Range), Session,
1616
TestName, TestTree,
1717
TextDocumentIdentifier,
18-
assertBool, assertFailure,
18+
assertFailure,
1919
defaultTestRunner,
2020
executeCodeAction,
2121
getCodeActions,
22-
goldenWithHaskellDoc, liftIO,
23-
openDoc, runSessionWithServer,
24-
testCase, testGroup, toEither,
25-
type (|?) (InR),
26-
waitForDiagnostics,
27-
waitForProgressDone, (@=?),
28-
(@?=))
22+
goldenWithHaskellDoc,
23+
knownBrokenForGhcVersions,
24+
liftIO, openDoc,
25+
runSessionWithServer, testCase,
26+
testGroup, toEither, type (|?),
27+
waitForAllProgressDone,
28+
waitForDiagnostics, (@?=))
2929
import Text.Regex.TDFA ((=~))
3030

3131
main :: IO ()
@@ -36,23 +36,21 @@ changeTypeSignaturePlugin = ChangeTypeSignature.descriptor "changeTypeSignature"
3636

3737
test :: TestTree
3838
test = testGroup "changeTypeSignature" [
39-
codeActionTest "TExpectedActual" 4 11
40-
, codeActionTest "TRigidType" 4 14
41-
, codeActionTest "TLocalBinding" 6 21
42-
, codeActionTest "TLocalBindingShadow1" 10 7
43-
, codeActionTest "TLocalBindingShadow2" 6 21
39+
testRegexes
40+
, codeActionTest "TExpectedActual" 4 11
41+
, knownBrokenForGhcVersions [GHC92] "Error Message in 9.2 does not provide enough info" $ codeActionTest "TRigidType" 4 14
42+
, codeActionTest "TLocalBinding" 7 22
43+
, codeActionTest "TLocalBindingShadow1" 11 8
44+
, codeActionTest "TLocalBindingShadow2" 7 22
4445
, codeActionProperties "TErrorGivenPartialSignature" [(4, 13)] $ \actions -> liftIO $ length actions @?= 0
45-
, testRegexes
4646
]
4747

4848
testRegexes :: TestTree
4949
testRegexes = testGroup "Regex Testing" [
5050
testRegexOne
5151
, testRegexTwo
52+
, testRegex921One
5253
]
53-
where
54-
regex1 = errorMessageRegexes !! 0
55-
regex2 = errorMessageRegexes !! 1
5654

5755
testRegexOne :: TestTree
5856
testRegexOne = testGroup "Regex One" [
@@ -76,6 +74,16 @@ testRegexTwo = testGroup "Regex Two" [
7674
where
7775
regex = errorMessageRegexes !! 1
7876

77+
-- test ghc-9.2.1 error message regex
78+
testRegex921One :: TestTree
79+
testRegex921One = testGroup "Regex One" [
80+
regexTest "ghc921-error1.txt" regex True
81+
, regexTest "ghc921-error2.txt" regex True
82+
, regexTest "ghc921-error3.txt" regex True
83+
]
84+
where
85+
regex = errorMessageRegexes !! 2
86+
7987
testDataDir :: FilePath
8088
testDataDir = "test" </> "testdata"
8189

@@ -84,7 +92,8 @@ goldenChangeSignature fp = goldenWithHaskellDoc changeTypeSignaturePlugin (fp <>
8492

8593
codeActionTest :: FilePath -> Int -> Int -> TestTree
8694
codeActionTest fp line col = goldenChangeSignature fp $ \doc -> do
87-
waitForDiagnostics -- code actions are triggered from Diagnostics
95+
void $ waitForDiagnostics -- code actions are triggered from Diagnostics
96+
void $ waitForAllProgressDone -- apparently some tests need this to get the CodeAction to show up
8897
actions <- getCodeActions doc (pointRange line col)
8998
foundActions <- findChangeTypeActions actions
9099
liftIO $ length foundActions @?= 1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module TLocalBinding where
22

3+
import Control.Monad (forM)
4+
35
local :: Int -> Int
4-
local x = let test :: [Int] -> Int
5-
test = head . reverse
6+
local x = let test :: t0 a0 -> (a0 -> m0 b0) -> m0 (t0 b0)
7+
test = forM
68
in x + 1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module TLocalBinding where
22

3+
import Control.Monad (forM)
4+
35
local :: Int -> Int
46
local x = let test :: Int -> Int
5-
test = head . reverse
7+
test = forM
68
in x + 1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module TLocalBindingShadow1 where
22

3+
import Control.Monad (forM)
4+
35
local :: Int -> Int
46
local x = let test :: Int -> Int
57
test = (+2)
68
in test x
79

8-
test :: [Double] -> Double
9-
test = head . reverse
10+
test :: [Double] -> (Double -> m0 b0) -> m0 [b0]
11+
test = forM
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module TLocalBindingShadow1 where
22

3+
import Control.Monad (forM)
4+
35
local :: Int -> Int
46
local x = let test :: Int -> Int
57
test = (+2)
68
in test x
79

8-
test :: Int -> Double
9-
test = head . reverse
10+
test :: [Double] -> Double
11+
test = forM

plugins/hls-change-type-signature-plugin/test/testdata/TLocalBindingShadow2.expected.hs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
module TLocalBindingShadow2 where
22

3+
import Control.Monad (forM)
4+
35
local :: Int -> Int
4-
local x = let test :: [Int] -> Int
5-
test = head . reverse
6+
local x = let test :: t0 a0 -> (a0 -> m0 b0) -> m0 (t0 b0)
7+
test = forM
68
in test x
79

810
test :: String -> String

plugins/hls-change-type-signature-plugin/test/testdata/TLocalBindingShadow2.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
module TLocalBindingShadow2 where
22

3+
import Control.Monad (forM)
4+
35
local :: Int -> Int
46
local x = let test :: Int -> Int
5-
test = head . reverse
7+
test = forM
68
in test x
79

810
test :: String -> String
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
• Couldn't match type ‘Data.Set.Internal.Set Int’ with ‘Int’
2+
Expected: Int -> [Int]
3+
Actual: Data.Set.Internal.Set Int -> [Int]
4+
• In the second argument of ‘(.)’, namely ‘toList’
5+
In the expression: head . toList
6+
In an equation for ‘test’: test = head . toList
7+
|
8+
83 | test = head . toList
9+
| ^^^^^^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
• Couldn't match type ‘b0 -> a0 -> b0’ with ‘Int’
2+
Expected: Int -> Int
3+
Actual: (b0 -> a0 -> b0) -> b0 -> t0 a0 -> b0
4+
• Probable cause: ‘foldl’ is applied to too few arguments
5+
In the expression: foldl
6+
In an equation for ‘test’: test = foldl
7+
|
8+
83 | test = foldl
9+
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
• Couldn't match type ‘[Int]’ with ‘Int’
2+
Expected: Int -> [Int]
3+
Actual: [Int] -> [Int]
4+
• In the second argument of ‘(.)’, namely ‘reverse’
5+
In the expression: head . reverse
6+
In an equation for ‘test’: test = head . reverse
7+
|
8+
84 | test = head . reverse
9+
|

0 commit comments

Comments
 (0)