Skip to content

Commit

Permalink
Merge pull request #105 from AiOO/bugfix/add-ancestors-of-packages
Browse files Browse the repository at this point in the history
Add ancestors of packages to packages in setup.py
  • Loading branch information
dahlia authored Mar 8, 2017
2 parents c724fea + 7f6a8af commit 03c012d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
14 changes: 9 additions & 5 deletions src/Nirum/Constructs/ModulePath.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ module Nirum.Constructs.ModulePath ( ModulePath ( ModuleName
, moduleName
, path
)
, ancestors
, fromFilePath
, fromIdentifiers
, hierarchy
, hierarchies
) where

import Data.Char (toLower)
import Data.Maybe (fromMaybe, mapMaybe)
import GHC.Exts (IsList (Item, fromList, toList))

import Data.Set (Set, insert, singleton)
import qualified Data.Set as S
import Data.Text (intercalate, pack)
import System.FilePath (splitDirectories, stripExtension)

Expand Down Expand Up @@ -55,9 +56,9 @@ fromFilePath filePath =
fileIdentifiers :: [Identifier]
fileIdentifiers = mapMaybe (fromText . pack) paths

ancestors :: ModulePath -> Set ModulePath
ancestors m@ModuleName {} = singleton m
ancestors m@(ModulePath parent _) = m `insert` ancestors parent
hierarchy :: ModulePath -> S.Set ModulePath
hierarchy m@ModuleName {} = S.singleton m
hierarchy m@(ModulePath parent _) = m `S.insert` hierarchy parent

instance IsList ModulePath where
type Item ModulePath = Identifier
Expand All @@ -66,3 +67,6 @@ instance IsList ModulePath where
(fromIdentifiers identifiers)
toList (ModuleName identifier) = [identifier]
toList (ModulePath path' identifier) = toList path' ++ [identifier]

hierarchies :: S.Set ModulePath -> S.Set ModulePath
hierarchies modulePaths = S.unions $ toList $ S.map hierarchy modulePaths
4 changes: 4 additions & 0 deletions src/Nirum/Package/ModuleSet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Nirum.Package.ModuleSet ( ImportError ( CircularImportError
, fromList
, fromMap
, keys
, keysSet
, length
, lookup
, null
Expand Down Expand Up @@ -67,6 +68,9 @@ null = M.null . toMap
keys :: ModuleSet -> [ModulePath]
keys = M.keys . toMap

keysSet :: ModuleSet -> S.Set ModulePath
keysSet = M.keysSet . toMap

lookup :: ModulePath -> ModuleSet -> Maybe Module
lookup path = M.lookup path . toMap

Expand Down
10 changes: 7 additions & 3 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Nirum.Targets.Python ( Code
, toAttributeName
, toClassName
, toImportPath
, toImportPaths
, toNamePair
, unionInstallRequires
) where
Expand Down Expand Up @@ -63,7 +64,7 @@ import Nirum.Constructs.Identifier ( Identifier
, toSnakeCaseText
, toString
)
import Nirum.Constructs.ModulePath (ModulePath, ancestors)
import Nirum.Constructs.ModulePath (ModulePath, hierarchy, hierarchies)
import Nirum.Constructs.Name (Name (Name))
import qualified Nirum.Constructs.Name as N
import Nirum.Constructs.Service ( Method ( Method
Expand Down Expand Up @@ -227,6 +228,9 @@ toAttributeName' = toAttributeName . N.facialName
toImportPath :: ModulePath -> T.Text
toImportPath = T.intercalate "." . map toAttributeName . toList

toImportPaths :: S.Set ModulePath -> [T.Text]
toImportPaths paths = S.toAscList $ S.map toImportPath $ hierarchies paths

toNamePair :: Name -> T.Text
toNamePair (Name f b) = [qq|('{toAttributeName f}', '{toSnakeCaseText b}')|]

Expand Down Expand Up @@ -919,7 +923,7 @@ setup(
| Author { email = Just e } <- authors metadata'
]
pPackages :: Code
pPackages = strings $ map toImportPath $ MS.keys $ modules package
pPackages = strings $ toImportPaths $ MS.keysSet $ modules package
pInstallRequires :: Code
pInstallRequires = strings $ S.toList deps
pPolyfillRequires :: Code
Expand Down Expand Up @@ -961,7 +965,7 @@ compilePackage' package =
initFiles :: [(FilePath, Either CompileError' Code)]
initFiles = [ (toFilename (sourceDirectory ver) mp', Right "")
| mp <- MS.keys (modules package)
, mp' <- S.elems (ancestors mp)
, mp' <- S.elems (hierarchy mp)
, ver <- versions
]
modules' :: [(FilePath, Either CompileError' (InstallRequires, Code))]
Expand Down
17 changes: 13 additions & 4 deletions test/Nirum/Constructs/ModulePathSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import Test.Hspec.Meta

import Nirum.Constructs (Construct (toCode))
import Nirum.Constructs.ModulePath ( ModulePath (ModuleName, ModulePath)
, ancestors
, hierarchy
, hierarchies
, fromFilePath
, fromIdentifiers
)
Expand Down Expand Up @@ -45,13 +46,21 @@ spec =
Just fooBarBaz
fromFilePath ("foo" </> "bar-baz2.nrm") `shouldBe` Just fooBarBaz2
fromFilePath ("foo" </> "bar_baz2.NRM") `shouldBe` Just fooBarBaz2
specify "ancestors" $ do
ancestors ["foo", "bar", "baz"] `shouldBe`
specify "hierarchy" $ do
hierarchy ["foo", "bar", "baz"] `shouldBe`
[ ["foo"]
, ["foo", "bar"]
, ["foo", "bar", "baz"]
]
ancestors ["foo"] `shouldBe` [["foo"]]
hierarchy ["foo"] `shouldBe` [["foo"]]
specify "hierarchies" $
hierarchies [ ["foo", "bar", "baz"], ["tar", "gz"] ] `shouldBe`
[ ["foo"]
, ["foo", "bar"]
, ["foo", "bar", "baz"]
, ["tar"]
, ["tar", "gz"]
]
context "Construct" $
specify "toCode" $ do
toCode foo `shouldBe` "foo"
Expand Down
5 changes: 5 additions & 0 deletions test/Nirum/Package/ModuleSetSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Data.Maybe (isNothing)
import Prelude hiding (length, lookup, null)

import qualified Data.Map.Strict as M
import qualified Data.Set as S
import Test.Hspec.Meta

import Nirum.Constructs.Annotation (empty)
Expand All @@ -23,6 +24,7 @@ import Nirum.Package.ModuleSet ( ImportError ( CircularImportError
, fromList
, fromMap
, keys
, keysSet
, length
, lookup
, null
Expand Down Expand Up @@ -109,6 +111,9 @@ spec =
specify "keys" $
sort (keys validModuleSet) `shouldBe`
sort [path | (path, _) <- validModules]
specify "keysSet" $
keysSet validModuleSet `shouldBe`
S.fromList [p | (p, _) <- validModules]
specify "lookup" $ do
let Just mod' = lookup ["foo", "bar"] validModuleSet
mod' `shouldBe` fooBarModule
Expand Down
14 changes: 13 additions & 1 deletion test/Nirum/Targets/PythonSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ import Nirum.Constructs.TypeExpression ( TypeExpression ( ListModifier
, TypeIdentifier
)
)
import Nirum.Package (Package, resolveBoundModule)
import Nirum.Package (Package (modules), resolveBoundModule)
import Nirum.Package.Metadata ( Author (Author, email, name, uri)
, Metadata (Metadata, authors, target, version)
, Target (compilePackage)
)
import qualified Nirum.Package.ModuleSet as MS
import Nirum.PackageSpec (createPackage)
import qualified Nirum.Targets.Python as PY
import Nirum.Targets.Python ( Source (Source)
Expand Down Expand Up @@ -349,6 +350,17 @@ spec = parallel $ forM_ versions $ \ (ver, typing) -> do
(3, 4) "ipaddress"
(req4 `unionInstallRequires` req5) `shouldBe` req6
(req5 `unionInstallRequires` req4) `shouldBe` req6
specify "toImportPath" $
PY.toImportPath ["foo", "bar"] `shouldBe` "foo.bar"
describe "Add ancestors of packages" $ do
let (Source pkg _) = makeDummySource $ Module [] Nothing
modulePaths = MS.keysSet $ modules pkg
specify "toImportPaths" $
PY.toImportPaths modulePaths `shouldBe` [ "foo"
, "foo.bar"
, "qux"
]


{-# ANN module ("HLint: ignore Functor law" :: String) #-}
{-# ANN module ("HLint: ignore Monad law, left identity" :: String) #-}
Expand Down
2 changes: 1 addition & 1 deletion test/python/setup_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def test_setup_metadata():
assert ['[email protected]'] == pkg['Author-email']
assert ['nirum'] == pkg['Requires']
assert set(pkg['Provides']) == {
'fixture.foo', 'fixture.foo.bar', 'fixture.qux',
'fixture', 'fixture.foo', 'fixture.foo.bar', 'fixture.qux',
}
assert ['0.3.0'] == pkg['Version']

0 comments on commit 03c012d

Please sign in to comment.