Skip to content

Commit

Permalink
Always output all packages in build info (#1126)
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMartinez authored Dec 3, 2023
1 parent c49c161 commit e0013d6
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/Spago/BuildInfo.purs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ writeBuildInfo = do
let
buildInfo =
{ pursVersion: Version.print purs.version
, packages: map mkPackageBuildInfo case workspace.selected of
Just p -> [ p ]
Nothing -> NEA.toUnfoldable $ Config.getWorkspacePackages workspace.packageSet
, packages: map mkPackageBuildInfo $ NEA.toUnfoldable $ Config.getWorkspacePackages workspace.packageSet
}
buildInfoString = mkBuildInfo buildInfo
writeIt = FS.writeTextFile buildInfoPath buildInfoString
Expand All @@ -56,7 +54,8 @@ writeBuildInfo = do
-- TODO: use tidy-codegen eventually
mkBuildInfo :: BuildInfo -> String
mkBuildInfo { packages, pursVersion } = String.joinWith "\n"
[ "module Spago.Generated.BuildInfo where"
[ "-- @inline export buildInfo always"
, "module Spago.Generated.BuildInfo where"
, ""
, "buildInfo :: { packages :: " <> recordType <> ", pursVersion :: String, spagoVersion :: String }"
, "buildInfo ="
Expand Down
19 changes: 16 additions & 3 deletions test/Prelude.purs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Data.String as String
import Effect.Aff as Aff
import Effect.Class.Console (log)
import Effect.Class.Console as Console
import Node.Path (dirname)
import Node.Path as Path
import Node.Process as Process
import Registry.PackageName as PackageName
Expand Down Expand Up @@ -91,7 +92,17 @@ shouldEqualStr
-> m Unit
shouldEqualStr v1 v2 =
when (v1 /= v2) do
fail $ "\n=====\n" <> v1 <> "\n=====\n\n=====\n " <> show v2 <> "\n=====\n"
fail $ Array.intercalate "\n"
[ ""
, "===== (Actual)"
, v1
, "====="
, ""
, "===== (Expected)"
, v2
, "====="
, ""
]

checkFixture :: String -> String -> Aff Unit
checkFixture filepath fixturePath = do
Expand Down Expand Up @@ -144,8 +155,8 @@ checkOutputsStr
-> Aff Unit
checkOutputsStr checkers =
check
{ stdout: maybe mempty (\exp act -> act `Assert.shouldEqual` exp) checkers.stdoutStr
, stderr: maybe mempty (\exp act -> act `Assert.shouldEqual` exp) checkers.stderrStr
{ stdout: maybe mempty (\exp act -> act `shouldEqualStr` exp) checkers.stdoutStr
, stderr: maybe mempty (\exp act -> act `shouldEqualStr` exp) checkers.stderrStr
, result: checkers.result
}

Expand All @@ -164,6 +175,8 @@ checkOutputs checkers execResult = do
overwriteSpecFile <- liftEffect $ map isJust $ Process.lookupEnv "SPAGO_TEST_ACCEPT"
if overwriteSpecFile then do
Console.log $ "Overwriting fixture at path: " <> fixtureFileExpected
let parentDir = dirname fixtureFileExpected
unlessM (FS.exists parentDir) $ FS.mkdirp parentDir
FS.writeTextFile fixtureFileExpected actual
else do
expected <- String.trim <$> FS.readTextFile fixtureFileExpected
Expand Down
3 changes: 3 additions & 0 deletions test/Spago/Build.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Spago.Core.Config as Config
import Spago.FS as FS
import Test.Spago.Build.Pedantic as Pedantic
import Test.Spago.Build.Polyrepo as BuildPolyrepo
import Test.Spago.Build.BuildInfo as BuildInfo
import Test.Spec (Spec)
import Test.Spec as Spec
import Test.Spec.Assertions as Assert
Expand Down Expand Up @@ -140,6 +141,8 @@ spec = Spec.around withTempDir do

BuildPolyrepo.spec

BuildInfo.spec

-- Spec.it "runs a --before command" \{ spago } -> do
-- spago [ "init" ] >>= shouldBeSuccess
-- let dumpFile = "testOutput"
Expand Down
141 changes: 141 additions & 0 deletions test/Spago/Build/BuildInfo.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
module Test.Spago.Build.BuildInfo where

import Test.Prelude

import Control.Monad.Error.Class as MonadError
import Data.Array as Array
import Data.DateTime.Instant as Instant
import Effect.Exception as Exception
import Node.Path as Path
import Registry.Version as Version
import Spago.Command.Init (DefaultConfigOptions(..))
import Spago.Command.Init as Init
import Spago.Core.Config as Config
import Spago.FS as FS
import Spago.Log (LogVerbosity(..))
import Spago.Purs (getPurs)
import Test.Spec (SpecT)
import Test.Spec as Spec

spec :: SpecT Aff TestDirs Identity Unit
spec =
Spec.describe "BuildInfo.purs" do

let
mkExpectedStdout { spago, rest } = do
let logOptions = { logOptions: { color: false, verbosity: LogQuiet, startingTime: Instant.fromDateTime bottom } }
purs <- runSpago logOptions getPurs
spagoResult <- spago [ "--version" ]
sVersion <- case spagoResult of
Left e -> MonadError.throwError $ Exception.error e.message
Right a -> pure a.stderr
pure
$ Array.intercalate "\n"
$
[ "pursVersion: " <> Version.print purs.version
, "spagoVersion: " <> sVersion
]
<> rest
pursModuleUsingBuildInfo packages =
[ "import Prelude"
, ""
, "import Spago.Generated.BuildInfo as BI"
, "import Effect.Console as Console"
, "import Effect (Effect)"
, ""
, "main :: Effect Unit"
, "main = do"
, " Console.log $ \"pursVersion: \" <> BI.buildInfo.pursVersion"
, " Console.log $ \"spagoVersion: \" <> BI.buildInfo.spagoVersion"
]
<> -- ensure all packages within the workspace are reference-able
( packages <#> \packageName ->
" Console.log $ \"" <> packageName <> ": \" <> BI.buildInfo.packages." <> packageName
)

Spec.describe "using generated 'BuildInfo.purs' file in single-package context" do

let
packageName = "foo"
srcAndTestContent = pursModuleUsingBuildInfo [ packageName ]

setupSinglePackage spago = do
spago [ "init", "--name", packageName ] >>= shouldBeSuccess
FS.writeTextFile (Path.concat [ "src", "Main.purs" ]) $ writeMain srcAndTestContent
FS.writeTextFile (Path.concat [ "test", "Test", "Main.purs" ]) $ writeTestMain srcAndTestContent

Spec.it ("'spago build' works") \{ spago } -> do
setupSinglePackage spago

spago [ "build" ] >>= shouldBeSuccess

Spec.it ("'spago build -p' works") \{ spago } -> do
setupSinglePackage spago

spago [ "build", "-p", packageName ] >>= shouldBeSuccess

let
runAndTestCommands = do
command <- [ "run", "test" ]
selected <- [ false, true ]
pure case selected of
false -> [ command ]
true -> [ command, "-p", packageName ]

for_ runAndTestCommands \command -> do
Spec.it ("'spago " <> Array.intercalate " " command <> " works") \{ spago } -> do
setupSinglePackage spago
expected <- mkExpectedStdout { spago, rest: [ "foo: 0.0.0" ] }
spago command >>= checkOutputsStr { stderrStr: Nothing, stdoutStr: Just expected, result: isRight }

Spec.describe "using generated 'BuildInfo.purs' file in multi-package context" do

let
packages = [ "foo", "bar", "baz" ]
setupPolyrepo = do
FS.writeYamlFile Config.configCodec "spago.yaml"
$ Init.defaultConfig'
$ WorkspaceOnly { setVersion: Just $ unsafeFromRight $ Version.parse "0.0.1" }
for_ packages \packageName -> do
FS.mkdirp packageName
FS.writeYamlFile Config.configCodec (Path.concat [ packageName, "spago.yaml" ])
$ mkPackageOnlyConfig { packageName, srcDependencies: [ "prelude", "effect", "console" ] }
[ configAddTestMain ]
let
src = Path.concat [ packageName, "src" ]
test = Path.concat [ packageName, "test", "Test" ]
fileContent = pursModuleUsingBuildInfo packages

FS.mkdirp src
FS.mkdirp test
FS.writeTextFile (Path.concat [ src, "Main.purs" ]) $ writePursFile
{ moduleName: mkSrcModuleName packageName
, rest: fileContent
}
FS.writeTextFile (Path.concat [ test, "Main.purs" ]) $ writePursFile
{ moduleName: mkTestModuleName packageName
, rest: fileContent
}

Spec.it ("'spago build' works") \{ spago } -> do
setupPolyrepo
spago [ "build" ] >>= shouldBeSuccess

Spec.before_ setupPolyrepo do

let packagesWithVersion = (\p -> p <> ": 0.0.0") <$> packages

for_ packages \package -> do
let
srcMain = mkSrcModuleName package
Spec.it ("'spago build -p " <> package <> "' works") \{ spago } -> do
spago [ "build", "-p", package ] >>= shouldBeSuccess

Spec.it ("'spago run -p " <> package <> " --main " <> srcMain <> "' works") \{ spago } -> do
expected <- mkExpectedStdout { spago, rest: packagesWithVersion }
spago [ "run", "-p", package, "--main", srcMain ] >>= checkOutputsStr { stderrStr: Nothing, stdoutStr: Just expected, result: isRight }

Spec.it ("'spago test -p " <> package <> "' works") \{ spago } -> do
expected <- mkExpectedStdout { spago, rest: packagesWithVersion }
spago [ "test", "-p", package ] >>= checkOutputsStr { stderrStr: Nothing, stdoutStr: Just expected, result: isRight }

0 comments on commit e0013d6

Please sign in to comment.