Skip to content

Commit

Permalink
Change default file paths for files in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Jun 20, 2022
1 parent 3ff9098 commit f79e970
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 36 deletions.
24 changes: 24 additions & 0 deletions src/Review/FileParser.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Review.FileParser exposing (parse)

import Elm.Parser as Parser
import Elm.Processing
import Elm.Syntax.File exposing (File)
import Review.Dependencies


{-| Parse source code into a AST.
-}
parse : String -> Result () File
parse source =
source
|> Parser.parse
|> Result.mapError (always ())
|> Result.map (Elm.Processing.process elmProcessContext)


elmProcessContext : Elm.Processing.ProcessContext
elmProcessContext =
Elm.Processing.init
|> Elm.Processing.addDependency Review.Dependencies.elmCore
|> Elm.Processing.addDependency Review.Dependencies.elmUrl
|> Elm.Processing.addDependency Review.Dependencies.elmParser
26 changes: 3 additions & 23 deletions src/Review/Project.elm
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ does not look at project information (like the `elm.json`, dependencies, ...).
-}

import Dict exposing (Dict)
import Elm.Parser as Parser
import Elm.Processing
import Elm.Project
import Elm.Syntax.File exposing (File)
import Elm.Syntax.File
import Elm.Syntax.ModuleName exposing (ModuleName)
import Elm.Syntax.Node as Node
import Review.Dependencies
import Review.FileParser as FileParser
import Review.Project.Dependency as Dependency exposing (Dependency)
import Review.Project.Internal as Internal exposing (Project)
import Vendor.Graph exposing (Graph)
Expand Down Expand Up @@ -107,7 +105,7 @@ and for which a parsing error will be reported when running [`the review functio
-}
addModule : { path : String, source : String } -> Project -> Project
addModule { path, source } project =
case parseSource source of
case FileParser.parse source of
Ok ast ->
let
osAgnosticPath : String
Expand Down Expand Up @@ -208,24 +206,6 @@ removeFileFromFilesThatFailedToParse path (Internal.Project project) =
}


{-| Parse source code into a AST
-}
parseSource : String -> Result () File
parseSource source =
source
|> Parser.parse
|> Result.mapError (always ())
|> Result.map (Elm.Processing.process elmProcessContext)


elmProcessContext : Elm.Processing.ProcessContext
elmProcessContext =
Elm.Processing.init
|> Elm.Processing.addDependency Review.Dependencies.elmCore
|> Elm.Processing.addDependency Review.Dependencies.elmUrl
|> Elm.Processing.addDependency Review.Dependencies.elmParser


{-| Get the list of modules in the project.
-}
modules : Project -> List ProjectModule
Expand Down
34 changes: 23 additions & 11 deletions src/Review/Test.elm
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import Elm.Syntax.Node as Node
import Elm.Syntax.Range exposing (Range)
import Expect exposing (Expectation)
import Review.Error as Error
import Review.FileParser as FileParser
import Review.Fix as Fix
import Review.Project as Project exposing (Project, ProjectModule)
import Review.Rule as Rule exposing (ReviewError, Rule)
Expand Down Expand Up @@ -349,18 +350,29 @@ runOnModulesWithProjectData project rule sources =
runOnModulesWithProjectDataHelp : Project -> Rule -> List String -> ReviewResult
runOnModulesWithProjectDataHelp project rule sources =
let
projectWithModules : Project
projectWithModules =
sources
|> List.indexedMap
(\index source ->
{ path = "src/File_" ++ String.fromInt index ++ ".elm"
, source = source
}
)
|> List.foldl Project.addModule project
( projectWithModules, modulesThatFailedToParse ) =
List.foldl
(\source ( accProject, accModulesThatFailedToParse ) ->
case FileParser.parse source of
Ok ast ->
( Project.addParsedModule
{ path = "src/" ++ String.join "/" (Module.moduleName (Node.value ast.moduleDefinition)) ++ ".elm"
, source = source
, ast = ast
}
accProject
, accModulesThatFailedToParse
)

Err _ ->
( accProject
, { source = source, path = "test/DummyFilePath.elm" } :: accModulesThatFailedToParse
)
)
( project, [] )
sources
in
case Project.modulesThatFailedToParse projectWithModules of
case modulesThatFailedToParse ++ Project.modulesThatFailedToParse projectWithModules of
{ source } :: _ ->
let
fileAndIndex : { source : String, index : Int }
Expand Down
30 changes: 28 additions & 2 deletions tests/Review/Rule/WithFilePathTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,43 @@ initContext =
all : Test
all =
Test.describe "withFilePath"
[ test "should not pass the elmJsonKey if the `elm.json` file does not exist" <|
[ test "should get the file path of a single module" <|
\() ->
"""module A exposing (..)
a = 1
"""
|> Review.Test.run rule
|> Review.Test.expectErrors
[ Review.Test.error
{ message = "File path: src/File_0.elm"
{ message = "File path: src/A.elm"
, details = [ "No details" ]
, under = "module A exposing (..)"
}
]
, test "should get the file paths of multiple modules" <|
\() ->
[ """module A exposing (..)
a = 1
""", """module A.B.C exposing (..)
a = 1
""" ]
|> Review.Test.runOnModules rule
|> Review.Test.expectErrorsForModules
[ ( "A"
, [ Review.Test.error
{ message = "File path: src/A.elm"
, details = [ "No details" ]
, under = "module A exposing (..)"
}
]
)
, ( "A.B.C"
, [ Review.Test.error
{ message = "File path: src/A/B/C.elm"
, details = [ "No details" ]
, under = "module A.B.C exposing (..)"
}
]
)
]
]

0 comments on commit f79e970

Please sign in to comment.