Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
Refactored some long names in the precompiler
Browse files Browse the repository at this point in the history
  • Loading branch information
NorfairKing committed Feb 10, 2016
1 parent 856f62f commit cd9241a
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 96 deletions.
44 changes: 44 additions & 0 deletions scripts/refactor.sh
@@ -0,0 +1,44 @@
source scripts/lib.sh

from="$1"
to="$2"

usage () {
echo "
refactor FROM TO
"
}

if [[ "$from" == "" || "$to" == "" ]]
then
usage
exit 0
fi

# Check if the string already exists somewhere
grep --color=auto --line-number --word-regexp "$to" --recursive --include \*.hs
if [[ "$?" == "0" ]]
then
warning "Destination already exists"
if ! promptN "Go on?"
then
exit 1
fi
else
good "Destination is not in use yet."
fi

grep --color=auto --line-number --word-regexp "$from" --recursive --include \*.hs
if ! promptY "Above are the matches for $from, continue?"
then
exit 1
fi


find src -type f -name "*.hs" -exec sed -i "s/\b$from\b/$to/g" {} \;

if promptY "make a commit out of it?"
then
git add .
git commit -m "Refactoring $from to $to"
fi
66 changes: 33 additions & 33 deletions src/Compiler/Internal.hs
Expand Up @@ -8,50 +8,50 @@ import Types
import Utils

preCompileChecks :: Card -> [PreCompileError]
preCompileChecks c = runIdentity $ execWriterT $ cleanCardCheck c
preCompileChecks c = runIdentity $ execWriterT $ cleanCard c

dirty :: String -> Precompiler ()
dirty s = tell ["Precompilation check failed: " ++ s]

cleanCardCheck :: Card -> Precompiler ()
cleanCardCheck (Card name d) = do
cleanCardNameCheck name
cleanDeclarationCheck d

cleanDeclarationCheck :: Declaration -> Precompiler ()
cleanDeclarationCheck (Deploy src dst _) = do
cleanFilePathCheck src
cleanFilePathCheck dst

cleanDeclarationCheck (SparkOff cr) = cleanCardReferenceCheck cr
cleanDeclarationCheck (IntoDir dir) = cleanFilePathCheck dir
cleanDeclarationCheck (OutofDir dir) = cleanFilePathCheck dir
cleanDeclarationCheck (DeployKindOverride _) = return () -- Nothing can go wrong.
cleanDeclarationCheck (Alternatives fs) = mapM_ cleanFilePathCheck fs
cleanDeclarationCheck (Block ds) = mapM_ cleanDeclarationCheck ds

cleanCardReferenceCheck :: CardReference -> Precompiler ()
cleanCardReferenceCheck (CardFile cfr) = cleanCardFileReferenceCheck cfr
cleanCardReferenceCheck (CardName cnr) = cleanCardNameReferenceCheck cnr

cleanCardFileReferenceCheck :: CardFileReference -> Precompiler ()
cleanCardFileReferenceCheck (CardFileReference fp mcnr) = do
cleanFilePathCheck fp
cleanCard :: Card -> Precompiler ()
cleanCard (Card name d) = do
cleanCardName name
cleanDeclaration d

cleanDeclaration :: Declaration -> Precompiler ()
cleanDeclaration (Deploy src dst _) = do
cleanFilePath src
cleanFilePath dst

cleanDeclaration (SparkOff cr) = cleanCardReference cr
cleanDeclaration (IntoDir dir) = cleanFilePath dir
cleanDeclaration (OutofDir dir) = cleanFilePath dir
cleanDeclaration (DeployKindOverride _) = return () -- Nothing can go wrong.
cleanDeclaration (Alternatives fs) = mapM_ cleanFilePath fs
cleanDeclaration (Block ds) = mapM_ cleanDeclaration ds

cleanCardReference :: CardReference -> Precompiler ()
cleanCardReference (CardFile cfr) = cleanCardFileReference cfr
cleanCardReference (CardName cnr) = cleanCardNameReference cnr

cleanCardFileReference :: CardFileReference -> Precompiler ()
cleanCardFileReference (CardFileReference fp mcnr) = do
cleanFilePath fp
case mcnr of
Nothing -> return ()
Just cnr -> cleanCardNameReferenceCheck cnr
Just cnr -> cleanCardNameReference cnr

cleanCardNameReferenceCheck :: CardNameReference -> Precompiler ()
cleanCardNameReferenceCheck (CardNameReference cn) = cleanCardNameCheck cn
cleanCardNameReference :: CardNameReference -> Precompiler ()
cleanCardNameReference (CardNameReference cn) = cleanCardName cn

cleanCardNameCheck :: CardName -> Precompiler ()
cleanCardNameCheck n
cleanCardName :: CardName -> Precompiler ()
cleanCardName n
| containsNewline n = dirty $ "Card name contains newline character(s): " ++ n
| otherwise = return ()

cleanFilePathCheck :: FilePath -> Precompiler ()
cleanFilePathCheck [] = dirty "Empty filepath"
cleanFilePathCheck fp
cleanFilePath :: FilePath -> Precompiler ()
cleanFilePath [] = dirty "Empty filepath"
cleanFilePath fp
| containsNewline fp =
dirty $ "Filepath contains newline character(s): " ++ fp
| containsMultipleConsequtiveSlashes fp =
Expand Down
8 changes: 4 additions & 4 deletions test/Compiler/TestUtils.hs
Expand Up @@ -14,16 +14,16 @@ cleanBy :: (a -> Precompiler ()) -> a -> Bool
cleanBy func a = null $ runPreCompiler $ func a

declarationClean :: Declaration -> IO ()
declarationClean d = d `shouldSatisfy` cleanBy cleanDeclarationCheck
declarationClean d = d `shouldSatisfy` cleanBy cleanDeclaration

declarationDirty :: Declaration -> IO ()
declarationDirty d = d `shouldNotSatisfy` cleanBy cleanDeclarationCheck
declarationDirty d = d `shouldNotSatisfy` cleanBy cleanDeclaration

filePathDirty :: FilePath -> IO ()
filePathDirty fp = fp `shouldNotSatisfy` cleanBy cleanFilePathCheck
filePathDirty fp = fp `shouldNotSatisfy` cleanBy cleanFilePath

filePathClean :: FilePath -> IO ()
filePathClean fp = fp `shouldSatisfy` cleanBy cleanFilePathCheck
filePathClean fp = fp `shouldSatisfy` cleanBy cleanFilePath

runPureCompiler :: SparkConfig -> PureCompiler a -> Either CompileError a
runPureCompiler c func = runIdentity $ runReaderT (runExceptT func) c
Expand Down
116 changes: 58 additions & 58 deletions test/CompilerSpec.hs
Expand Up @@ -28,155 +28,155 @@ spec = parallel $ do

precompileSpec :: Spec
precompileSpec = describe "pre-compilation" $ do
cleanContentCheckSpec
cleanContentSpec

cleanContentCheckSpec :: Spec
cleanContentCheckSpec = do
let validFp = arbitrary `suchThat` cleanBy cleanFilePathCheck
cleanContentSpec :: Spec
cleanContentSpec = do
let validFp = arbitrary `suchThat` cleanBy cleanFilePath

describe "cleanCardCheck" $ do
describe "cleanCard" $ do
it "doesn't report any card with valid content and a valid name" $ do
forAll (arbitrary `suchThat` cleanBy cleanCardNameCheck) $ \cn ->
forAll (arbitrary `suchThat` cleanBy cleanDeclarationCheck) $ \cc ->
Card cn cc `shouldSatisfy` cleanBy cleanCardCheck
forAll (arbitrary `suchThat` cleanBy cleanCardName) $ \cn ->
forAll (arbitrary `suchThat` cleanBy cleanDeclaration) $ \cc ->
Card cn cc `shouldSatisfy` cleanBy cleanCard

describe "cleanCardNameCheck" $ do
describe "cleanCardName" $ do
pend

it "doesn't report an emty card name" $ do
"" `shouldSatisfy` cleanBy cleanCardNameCheck
"" `shouldSatisfy` cleanBy cleanCardName

it "reports card names with newlines" $ do
forAll (arbitrary `suchThat` containsNewlineCharacter) $ \s ->
s `shouldNotSatisfy` cleanBy cleanCardNameCheck
s `shouldNotSatisfy` cleanBy cleanCardName

describe "cleanDeclarationCheck" $ do
describe "cleanDeclaration" $ do
describe "Deploy" $ do
it "doesn't report Deploy declarations with valid filepaths" $ do
forAll validFp $ \src ->
forAll validFp $ \dst ->
forAll arbitrary $ \kind ->
Deploy src dst kind `shouldSatisfy` cleanBy cleanDeclarationCheck
Deploy src dst kind `shouldSatisfy` cleanBy cleanDeclaration

it "reports Deploy declarations with an invalid source" $ do
forAll (arbitrary `suchThat` (not . cleanBy cleanFilePathCheck)) $ \src ->
forAll (arbitrary `suchThat` (not . cleanBy cleanFilePath)) $ \src ->
forAll validFp $ \dst ->
forAll arbitrary $ \kind ->
Deploy src dst kind `shouldNotSatisfy` cleanBy cleanDeclarationCheck
Deploy src dst kind `shouldNotSatisfy` cleanBy cleanDeclaration

it "reports Deploy declarations with an invalid destination" $ do
forAll validFp $ \src ->
forAll (arbitrary `suchThat` (not . cleanBy cleanFilePathCheck)) $ \dst ->
forAll (arbitrary `suchThat` (not . cleanBy cleanFilePath)) $ \dst ->
forAll arbitrary $ \kind ->
Deploy src dst kind `shouldNotSatisfy` cleanBy cleanDeclarationCheck
Deploy src dst kind `shouldNotSatisfy` cleanBy cleanDeclaration

pend

describe "SparkOff" $ do
it "reports SparkOff declarations with an invalid card reference" $ do
forAll (arbitrary `suchThat` (not . cleanBy cleanCardReferenceCheck)) $ \cr ->
SparkOff cr `shouldNotSatisfy` cleanBy cleanDeclarationCheck
forAll (arbitrary `suchThat` (not . cleanBy cleanCardReference)) $ \cr ->
SparkOff cr `shouldNotSatisfy` cleanBy cleanDeclaration

it "doesn't report SparkOff declarations with a valid card reference" $ do
forAll (arbitrary `suchThat` cleanBy cleanCardReferenceCheck) $ \cr ->
SparkOff cr `shouldSatisfy` cleanBy cleanDeclarationCheck
forAll (arbitrary `suchThat` cleanBy cleanCardReference) $ \cr ->
SparkOff cr `shouldSatisfy` cleanBy cleanDeclaration

pend

describe "IntoDir" $ do
it "reports IntoDir declarations with an invalid filepath" $ do
forAll (arbitrary `suchThat` (not . cleanBy cleanFilePathCheck)) $ \fp ->
IntoDir fp `shouldNotSatisfy` cleanBy cleanDeclarationCheck
forAll (arbitrary `suchThat` (not . cleanBy cleanFilePath)) $ \fp ->
IntoDir fp `shouldNotSatisfy` cleanBy cleanDeclaration

it "doesn't report IntoDir declarations with a valid filepath" $ do
forAll (arbitrary `suchThat` cleanBy cleanFilePathCheck) $ \fp ->
IntoDir fp `shouldSatisfy` cleanBy cleanDeclarationCheck
forAll (arbitrary `suchThat` cleanBy cleanFilePath) $ \fp ->
IntoDir fp `shouldSatisfy` cleanBy cleanDeclaration

pend

describe "OutofDir" $ do
it "reports OutofDir declarations with an invalid filepath" $ do
forAll (arbitrary `suchThat` (not . cleanBy cleanFilePathCheck)) $ \fp ->
OutofDir fp `shouldNotSatisfy` cleanBy cleanDeclarationCheck
forAll (arbitrary `suchThat` (not . cleanBy cleanFilePath)) $ \fp ->
OutofDir fp `shouldNotSatisfy` cleanBy cleanDeclaration

it "doesn't report OutofDir declarations with a valid filepath" $ do
forAll (arbitrary `suchThat` cleanBy cleanFilePathCheck) $ \fp ->
OutofDir fp `shouldSatisfy` cleanBy cleanDeclarationCheck
forAll (arbitrary `suchThat` cleanBy cleanFilePath) $ \fp ->
OutofDir fp `shouldSatisfy` cleanBy cleanDeclaration

pend

describe "DeployKindOverride" $ do
it "doesn't report any deployment kind override declarations" $ do
forAll arbitrary $ \kind ->
DeployKindOverride kind `shouldSatisfy` cleanBy cleanDeclarationCheck
DeployKindOverride kind `shouldSatisfy` cleanBy cleanDeclaration

pend

describe "Alternatives" $ do
it "reports alternatives declarations with as much as a single invalid filepath" $ do
forAll (arbitrary `suchThat` (any $ not . cleanBy cleanFilePathCheck)) $ \fs ->
Alternatives fs `shouldNotSatisfy` cleanBy cleanDeclarationCheck
forAll (arbitrary `suchThat` (any $ not . cleanBy cleanFilePath)) $ \fs ->
Alternatives fs `shouldNotSatisfy` cleanBy cleanDeclaration

it "doesn't report alternatives declarations with valid filepaths" $ do
forAll (arbitrary `suchThat` (all $ cleanBy cleanFilePathCheck)) $ \fs ->
Alternatives fs `shouldSatisfy` cleanBy cleanDeclarationCheck
forAll (arbitrary `suchThat` (all $ cleanBy cleanFilePath)) $ \fs ->
Alternatives fs `shouldSatisfy` cleanBy cleanDeclaration

pend

describe "Block" $ do
it "reports block declarations with as much as a single invalid declaration inside" $ do
forAll (arbitrary `suchThat` (any $ not . cleanBy cleanDeclarationCheck)) $ \ds ->
Block ds `shouldNotSatisfy` cleanBy cleanDeclarationCheck
forAll (arbitrary `suchThat` (any $ not . cleanBy cleanDeclaration)) $ \ds ->
Block ds `shouldNotSatisfy` cleanBy cleanDeclaration

it "doesn't report any block declarations with valid declarations inside" $ do
forAll (arbitrary `suchThat` (all $ cleanBy cleanDeclarationCheck)) $ \ds ->
Block ds `shouldSatisfy` cleanBy cleanDeclarationCheck
forAll (arbitrary `suchThat` (all $ cleanBy cleanDeclaration)) $ \ds ->
Block ds `shouldSatisfy` cleanBy cleanDeclaration


pend

describe "cleanCardReferenceCheck" $ do
it "works the same as cleanCardNameCheck separately" $ do
describe "cleanCardReference" $ do
it "works the same as cleanCardName separately" $ do
forAll arbitrary $ \cnr ->
cleanBy cleanCardNameReferenceCheck cnr === cleanBy cleanCardReferenceCheck (CardName cnr)
cleanBy cleanCardNameReference cnr === cleanBy cleanCardReference (CardName cnr)

it "works the same as cleanCardFileCheck separately" $ do
it "works the same as cleanCardFile separately" $ do
forAll arbitrary $ \cfr ->
cleanBy cleanCardFileReferenceCheck cfr === cleanBy cleanCardReferenceCheck (CardFile cfr)
cleanBy cleanCardFileReference cfr === cleanBy cleanCardReference (CardFile cfr)

pend

describe "cleanCardNameReferenceCheck" $ do
describe "cleanCardNameReference" $ do
it "reports card name references with an invalid card name" $ do
forAll (arbitrary `suchThat` (not . cleanBy cleanCardNameCheck)) $ \cn ->
CardNameReference cn `shouldNotSatisfy` cleanBy cleanCardNameReferenceCheck
forAll (arbitrary `suchThat` (not . cleanBy cleanCardName)) $ \cn ->
CardNameReference cn `shouldNotSatisfy` cleanBy cleanCardNameReference

it "doesn't report card name references with a valid card name" $ do
forAll (arbitrary `suchThat` cleanBy cleanCardNameCheck) $ \cn ->
CardNameReference cn `shouldSatisfy` cleanBy cleanCardNameReferenceCheck
forAll (arbitrary `suchThat` cleanBy cleanCardName) $ \cn ->
CardNameReference cn `shouldSatisfy` cleanBy cleanCardNameReference

pend

describe "cleanCardFileReferenceCheck" $ do
describe "cleanCardFileReference" $ do
it "reports card file references with an invalid filepath" $ do
forAll (arbitrary `suchThat` (not . cleanBy cleanFilePathCheck)) $ \fp ->
forAll (arbitrary `suchThat` (not . cleanBy cleanFilePath)) $ \fp ->
forAll arbitrary $ \cn ->
CardFileReference fp cn `shouldNotSatisfy` cleanBy cleanCardFileReferenceCheck
CardFileReference fp cn `shouldNotSatisfy` cleanBy cleanCardFileReference

it "reports card file references with an invalid card name" $ do
forAll arbitrary $ \fp ->
forAll (arbitrary `suchThat` (not . cleanBy cleanCardNameReferenceCheck)) $ \cn ->
CardFileReference fp (Just cn) `shouldNotSatisfy` cleanBy cleanCardFileReferenceCheck
forAll (arbitrary `suchThat` (not . cleanBy cleanCardNameReference)) $ \cn ->
CardFileReference fp (Just cn) `shouldNotSatisfy` cleanBy cleanCardFileReference

it "doesn't report card file references with a valid card name reference and valid filepath" $ do
forAll (arbitrary `suchThat` cleanBy cleanFilePathCheck) $ \fp ->
forAll (arbitrary `suchThat` cleanBy cleanCardNameReferenceCheck) $ \cn ->
CardFileReference fp (Just cn) `shouldSatisfy` cleanBy cleanCardFileReferenceCheck
forAll (arbitrary `suchThat` cleanBy cleanFilePath) $ \fp ->
forAll (arbitrary `suchThat` cleanBy cleanCardNameReference) $ \cn ->
CardFileReference fp (Just cn) `shouldSatisfy` cleanBy cleanCardFileReference

pend


describe "cleanFilePathCheck" $ do
describe "cleanFilePath" $ do
it "reports empty an filepath" $ do
filePathDirty []

Expand Down Expand Up @@ -214,7 +214,7 @@ singleCompileDecSpec = describe "compileDec" $ do
let nonNull = arbitrary `suchThat` (not . null)
let validFilePath = nonNull `suchThat` (not . containsNewlineCharacter)
let easyFilePath = validFilePath `suchThat` (not . isPrefixOf ".")
let validFp = arbitrary `suchThat` cleanBy cleanFilePathCheck
let validFp = arbitrary `suchThat` cleanBy cleanFilePath

describe "Deploy" $ do
it "uses the exact right text in source and destination when given valid filepaths without a leading dot" $ do
Expand Down
2 changes: 1 addition & 1 deletion test/SeedSpec.hs
Expand Up @@ -17,7 +17,7 @@ spec = parallel $ do
pureSeedSpec :: Spec
pureSeedSpec = describe "seed" $ do
it "ensures that sources are absolute if the seed is an absolute path" $ do
forAll (arbitrary `suchThat` cleanBy cleanFilePathCheck `suchThat` isAbsolute) $ \fp ->
forAll (arbitrary `suchThat` cleanBy cleanFilePath `suchThat` isAbsolute) $ \fp ->
forAll arbitrary $ \ds ->
all (\d -> all (isAbsolute) $ deployment_srcs d) $ seed fp ds

Expand Down

0 comments on commit cd9241a

Please sign in to comment.