Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit b8d9bc3

Browse files
committed
Make sure we only import directory
1 parent 1f7b582 commit b8d9bc3

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

src/SqlSquared/Parser.purs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ import_
412412
import_ = asErrorMessage "import declaration" do
413413
_ ← keyword "import"
414414
s ← ident
415-
pure $ Sig.Import s
415+
path ← Sig.parseAnyDirPath P.fail s
416+
pure $ Sig.Import path
416417

417418
variable m t. SqlParser' m t
418419
variable = C.vari <$> variableString

src/SqlSquared/Signature.purs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module SqlSquared.Signature
1111
, SqlDeclF(..)
1212
, SqlQueryF(..)
1313
, SqlModuleF(..)
14+
, AnyDirPath
15+
, parseAnyDirPath
1416
, printSqlF
1517
, printSqlDeclF
1618
, printSqlQueryF
@@ -58,6 +60,8 @@ import Data.Maybe (Maybe(..))
5860
import Data.Monoid (mempty)
5961
import Data.Newtype (class Newtype)
6062
import Data.NonEmpty ((:|))
63+
import Data.Path.Pathy as Pt
64+
import Data.Path.Pathy.Gen as PtGen
6165
import Data.Ord (class Ord1, compare1)
6266
import Data.String as S
6367
import Data.String.Gen as GenS
@@ -140,8 +144,20 @@ data SqlF literal a
140144
| Select (SelectR a)
141145
| Parens a
142146

147+
type AnyDirPath = E.Either (Pt.AbsDir Pt.Unsandboxed) (Pt.RelDir Pt.Unsandboxed)
148+
149+
printAnyDirPath :: AnyDirPath -> String
150+
printAnyDirPath = E.either Pt.unsafePrintPath Pt.unsafePrintPath
151+
152+
parseAnyDirPath :: forall m. Applicative m => (forall a. String -> m a) -> String -> m AnyDirPath
153+
parseAnyDirPath fail = Pt.parsePath
154+
(pure ∘ E.Right)
155+
(pure ∘ E.Left)
156+
(const $ fail "incorrect directory path")
157+
(const $ fail "incorrect directory path")
158+
143159
data SqlDeclF a
144-
= Import String
160+
= Import AnyDirPath
145161
| FunctionDecl (FunctionDeclR a)
146162

147163
newtype SqlModuleF a =
@@ -504,8 +520,8 @@ printSqlDeclF = case _ of
504520
<> "(" <> F.intercalate ", " (append ":"ID.printIdent <$> args) <> ") BEGIN "
505521
<> body
506522
<> " END"
507-
Import s
508-
"IMPORT " <> ID.printIdent s
523+
Import path
524+
"IMPORT " <> ID.printIdent (printAnyDirPath path)
509525

510526
printSqlQueryF Algebra SqlQueryF String
511527
printSqlQueryF (Query decls expr) = F.intercalate "; " $ L.snoc (printSqlDeclF <$> decls) expr
@@ -590,9 +606,9 @@ encodeJsonSqlDeclF = case _ of
590606
J.~> "args" J.:= args
591607
J.~> "body" J.:= body
592608
J.~> J.jsonEmptyObject
593-
Import s
609+
Import path
594610
"tag" J.:= "import"
595-
J.~> "value" J.:= s
611+
J.~> "value" J.:= printAnyDirPath path
596612
J.~> J.jsonEmptyObject
597613

598614
encodeJsonSqlQueryF Algebra SqlQueryF J.Json
@@ -714,7 +730,8 @@ decodeJsonSqlDeclF = J.decodeJson >=> \obj → do
714730

715731
decodeImport obj = do
716732
v ← obj J..? "value"
717-
pure $ Import v
733+
path ← parseAnyDirPath E.Left v
734+
pure $ Import path
718735

719736
decodeJsonSqlQueryF CoalgebraM (E.Either String) SqlQueryF J.Json
720737
decodeJsonSqlQueryF = J.decodeJson >=> \obj → do
@@ -879,7 +896,10 @@ genFunctionDecl n = do
879896
pure $ FunctionDecl { ident, args, body: n - 1 }
880897

881898
genImport a. Gen.Gen (SqlDeclF a)
882-
genImport = Import <$> genIdent
899+
genImport = map Import
900+
$ Gen.oneOf
901+
$ (Pt.unsandbox >>> E.Left <$> PtGen.genAbsDirPath)
902+
:| [Pt.unsandbox >>> E.Right <$> PtGen.genRelDirPath]
883903

884904
genIdent Gen.Gen String
885905
genIdent = do

test/src/Gen.purs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ module Test.Gen where
22

33
import Prelude
44

5-
import Control.Monad.Eff (Eff, foreachE)
5+
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE)
77
import Control.Monad.Eff.Exception (EXCEPTION)
88
import Control.Monad.Eff.Random (RANDOM)
9-
import Data.Array ((..))
109
import Data.Either as E
1110
import SqlSquared (SqlQuery, arbitrarySqlQueryOfSize, decodeJsonQuery, encodeJsonQuery, printQuery, tokenize)
1211
import Test.QuickCheck ((<?>))

test/src/Parse.purs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ parseFail s =
4040
E.Left err → pure unit
4141
E.Right (sql SqlQuery) → Assert.assert s false
4242

43+
parseFailWith e. String String TestSuite (testOutput Console.TESTOUTPUT | e)
44+
parseFailWith s err =
45+
test "parse/failWith"
46+
case parseQuery s of
47+
E.Left err' →
48+
if show err' == err
49+
then pure unit
50+
else Assert.assert
51+
("expected query:" <> s <>
52+
"\n\n to fail input error: " <> err <>
53+
"\n\n but instead fot error: " <> show err')
54+
false
55+
E.Right (sql SqlQuery) →
56+
Assert.assert
57+
("expected to fail with:" <> err <>
58+
"\n\tbut input query:" <> s <>
59+
"\n\twas parsed as:" <> printQuery sql)
60+
false
61+
4362
testSuite e. TestSuite (testOutput Console.TESTOUTPUT | e)
4463
testSuite = suite "parsers" do
4564
testSuite1
@@ -51,6 +70,14 @@ testSuite = suite "parsers" do
5170

5271
testSuite1 e. TestSuite (testOutput Console.TESTOUTPUT | e)
5372
testSuite1 = do
73+
parseFailWith """
74+
import `/path/To/Your/File/myModule`; SELECT id("HELLO")
75+
""" "(ParseError \"incorrect directory path\" (Position { line: 2, column: 12 }))"
76+
77+
parseSucc """
78+
import `/path/To/Your/File/myModule/`; SELECT id("HELLO")
79+
"""
80+
5481
parseSucc """
5582
select a from `/f` where b is not between 1 and (2..3)
5683
"""
@@ -121,6 +148,10 @@ testSuite1 = do
121148
"""
122149

123150
parseSucc """
151+
import `foo/`; select * from `/test`
152+
"""
153+
154+
parseFail """
124155
import foo; select * from `/test`
125156
"""
126157

0 commit comments

Comments
 (0)