-
Notifications
You must be signed in to change notification settings - Fork 221
Fix mainInParentDirectory failing when initdb is not on PATH #2398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amitaibu
wants to merge
10
commits into
master
Choose a base branch
from
fix-2397-mainInParentDirectory-initdb
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4ca03e4
Fix Postgres init commands in wrapped dev server mode
amitaibu 6149b66
Resolve IHPSchema path in ghci parent-directory mode
amitaibu 9c9ddd2
Fix mainInParentDirectory failing when initdb is not on PATH (#2400)
Copilot 095d896
Fix PostgresSpec test failing in nix build sandbox
amitaibu 5908473
Merge branch 'master' into fix-2397-mainInParentDirectory-initdb
amitaibu 557852a
Remove redundant fallback candidates from Paths_ihp_ide
amitaibu b432aa0
Address PR review feedback
amitaibu 20c65d7
Add temporary to ihp-ide test build-depends
amitaibu c67dea2
Fix IHPSchema.sql path assertion for Nix builds
amitaibu 2e982b8
Merge branch 'master' into fix-2397-mainInParentDirectory-initdb
amitaibu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| module Test.IDE.PostgresSpec where | ||
|
|
||
| import IHP.Prelude | ||
| import Test.Hspec | ||
| import IHP.IDE.Postgres (withBuiltinOrDevenvPostgres) | ||
| import IHP.IDE.Types (Context (..)) | ||
| import IHP.IDE.PortConfig (PortConfig (..)) | ||
| import qualified IHP.Log as Log | ||
| import Control.Concurrent.MVar (takeMVar) | ||
| import qualified Control.Concurrent.Chan.Unagi as Queue | ||
| import qualified Control.Exception.Safe as Exception | ||
| import qualified Data.Map as Map | ||
| import Data.Default (def) | ||
| import qualified Data.Text as Text | ||
| import qualified Data.Text.IO as TextIO | ||
| import qualified Network.Socket as Socket | ||
| import qualified System.Directory as Directory | ||
| import qualified System.Environment as Env | ||
| import qualified System.IO.Temp as Temp | ||
| import qualified System.Process as Process | ||
| import qualified System.Timeout as Timeout | ||
|
|
||
| tests :: Spec | ||
| tests = describe "IHP.IDE.Postgres" do | ||
| it "runs postgres setup through direnv in wrapped mode" do | ||
| withTemporaryTestDirectory \testDir -> do | ||
| let scriptsDir = testDir <> "/bin" | ||
| let commandLog = testDir <> "/commands.log" | ||
| path <- fromMaybe "" <$> Env.lookupEnv "PATH" | ||
|
|
||
| writeExecutable (scriptsDir <> "/direnv") direnvScript | ||
| writeExecutable (scriptsDir <> "/initdb") initdbScript | ||
| writeExecutable (scriptsDir <> "/createdb") createdbScript | ||
| writeExecutable (scriptsDir <> "/psql") psqlScript | ||
| writeExecutable (scriptsDir <> "/postgres") postgresScript | ||
|
|
||
| withEnvVar "PATH" (scriptsDir <> ":" <> path) do | ||
| withEnvVar "IHP_DEVENV" "0" do | ||
| withEnvVar "IHP_TEST_COMMAND_LOG" commandLog do | ||
| withEnvVar "IHP_LIB" (testDir <> "/IHP/ihp-ide/lib/IHP") do | ||
| withTestContext True do | ||
| withCurrentDirectory testDir do | ||
| withBuiltinOrDevenvPostgres \databaseIsReady _ _ -> do | ||
| ready <- Timeout.timeout (2 * 1000 * 1000) (takeMVar databaseIsReady) | ||
| ready `shouldBe` Just () | ||
|
|
||
| commandLogContent <- TextIO.readFile commandLog | ||
| let commandLines = Text.lines commandLogContent | ||
| commandLines `shouldSatisfy` any ("exec . initdb " `Text.isPrefixOf`) | ||
| commandLines `shouldSatisfy` any ("exec . createdb " `Text.isPrefixOf`) | ||
| commandLines `shouldSatisfy` any ("exec . psql " `Text.isPrefixOf`) | ||
| -- The exact path depends on the environment (dev uses IHP_LIB, | ||
| -- Nix uses Cabal's data-dir). The psql stub validates the file exists; | ||
| -- here we just verify an absolute path to IHPSchema.sql is used. | ||
| commandLines `shouldSatisfy` any (\line -> "-f /" `Text.isInfixOf` line && "IHPSchema.sql" `Text.isInfixOf` line) | ||
| commandLines `shouldSatisfy` any ("-f Application/Schema.sql" `Text.isInfixOf`) | ||
| commandLines `shouldSatisfy` any ("-f Application/Fixtures.sql" `Text.isInfixOf`) | ||
| length (filter ("exec . postgres " `Text.isPrefixOf`) commandLines) `shouldSatisfy` (>= 2) | ||
|
|
||
| withEnvVar :: String -> String -> IO a -> IO a | ||
| withEnvVar key value action = do | ||
| oldValue <- Env.lookupEnv key | ||
| Exception.bracket_ | ||
| (Env.setEnv key value) | ||
| (restoreEnvVar oldValue) | ||
| action | ||
| where | ||
| restoreEnvVar (Just oldValue) = Env.setEnv key oldValue | ||
| restoreEnvVar Nothing = Env.unsetEnv key | ||
|
|
||
| withTemporaryTestDirectory :: (FilePath -> IO a) -> IO a | ||
| withTemporaryTestDirectory callback = do | ||
| Temp.withSystemTempDirectory "ihp-test-postgres" \testDir -> do | ||
| Directory.createDirectoryIfMissing True (testDir <> "/Application") | ||
| Directory.createDirectoryIfMissing True (testDir <> "/bin") | ||
| Directory.createDirectoryIfMissing True (testDir <> "/IHP/ihp-ide/data") | ||
| TextIO.writeFile (testDir <> "/Application/Schema.sql") "CREATE TABLE test_table (id UUID PRIMARY KEY);\n" | ||
| TextIO.writeFile (testDir <> "/Application/Fixtures.sql") "" | ||
| TextIO.writeFile (testDir <> "/IHP/ihp-ide/data/IHPSchema.sql") "CREATE EXTENSION IF NOT EXISTS pgcrypto;\n" | ||
| callback testDir | ||
|
|
||
| withCurrentDirectory :: FilePath -> IO a -> IO a | ||
| withCurrentDirectory workingDirectory callback = do | ||
| oldDirectory <- Directory.getCurrentDirectory | ||
| Exception.bracket_ | ||
| (Directory.setCurrentDirectory workingDirectory) | ||
| (Directory.setCurrentDirectory oldDirectory) | ||
| callback | ||
|
|
||
|
|
||
| withTestContext :: Bool -> ((?context :: Context) => IO a) -> IO a | ||
| withTestContext wrapWithDirenv callback = Exception.bracket createContext cleanupContext runCallback | ||
| where | ||
| createContext = do | ||
| logger <- Log.newLogger def | ||
| (ghciInChan, ghciOutChan) <- Queue.newChan | ||
| liveReloadClients <- newIORef Map.empty | ||
| lastSchemaCompilerError <- newIORef Nothing | ||
| (appSocket, helperSocket) <- Socket.socketPair Socket.AF_UNIX Socket.Stream Socket.defaultProtocol | ||
| Socket.close helperSocket | ||
| let portConfig = PortConfig { appPort = 8000, toolServerPort = 8001 } | ||
| let isDebugMode = False | ||
| let context = Context { portConfig, isDebugMode, logger, ghciInChan, ghciOutChan, wrapWithDirenv, liveReloadClients, lastSchemaCompilerError, appSocket } | ||
| pure context | ||
|
|
||
| cleanupContext context = do | ||
| Socket.close context.appSocket | ||
| context.logger.cleanup | ||
|
|
||
| runCallback context = do | ||
| let ?context = context | ||
| callback | ||
|
|
||
| writeExecutable :: FilePath -> Text -> IO () | ||
| writeExecutable filePath content = do | ||
| TextIO.writeFile filePath content | ||
| Process.callProcess "chmod" ["+x", filePath] | ||
|
|
||
| direnvScript :: Text | ||
| direnvScript = Text.unlines | ||
| [ "#!/bin/sh" | ||
| , "set -eu" | ||
| , "if [ \"${1:-}\" != \"exec\" ] || [ \"${2:-}\" != \".\" ]; then" | ||
| , " echo \"unexpected direnv invocation: $*\" >&2" | ||
| , " exit 1" | ||
| , "fi" | ||
| , "echo \"$*\" >> \"$IHP_TEST_COMMAND_LOG\"" | ||
| , "shift 2" | ||
| , "export IHP_TEST_CALLED_VIA_DIRENV=1" | ||
| , "exec \"$@\"" | ||
| ] | ||
|
|
||
| initdbScript :: Text | ||
| initdbScript = Text.unlines | ||
| [ "#!/bin/sh" | ||
| , "set -eu" | ||
| , "if [ \"${IHP_TEST_CALLED_VIA_DIRENV:-0}\" != \"1\" ]; then" | ||
| , " echo \"initdb called without direnv\" >&2" | ||
| , " exit 1" | ||
| , "fi" | ||
| , "mkdir -p \"$1\"" | ||
| ] | ||
|
|
||
| createdbScript :: Text | ||
| createdbScript = Text.unlines | ||
| [ "#!/bin/sh" | ||
| , "set -eu" | ||
| , "if [ \"${IHP_TEST_CALLED_VIA_DIRENV:-0}\" != \"1\" ]; then" | ||
| , " echo \"createdb called without direnv\" >&2" | ||
| , " exit 1" | ||
| , "fi" | ||
| ] | ||
|
|
||
| psqlScript :: Text | ||
| psqlScript = Text.unlines | ||
| [ "#!/bin/sh" | ||
| , "set -eu" | ||
| , "if [ \"${IHP_TEST_CALLED_VIA_DIRENV:-0}\" != \"1\" ]; then" | ||
| , " echo \"psql called without direnv\" >&2" | ||
| , " exit 1" | ||
| , "fi" | ||
| , "# Verify that -f file targets exist" | ||
| , "while [ $# -gt 0 ]; do" | ||
| , " case \"$1\" in" | ||
| , " -f) shift; if [ ! -f \"$1\" ]; then echo \"psql: file not found: $1\" >&2; exit 1; fi;;" | ||
| , " esac" | ||
| , " shift" | ||
| , "done" | ||
| ] | ||
|
|
||
| postgresScript :: Text | ||
| postgresScript = Text.unlines | ||
| [ "#!/bin/sh" | ||
| , "set -eu" | ||
| , "if [ \"${IHP_TEST_CALLED_VIA_DIRENV:-0}\" != \"1\" ]; then" | ||
| , " echo \"postgres called without direnv\" >&2" | ||
| , " exit 1" | ||
| , "fi" | ||
| , "echo \"database system is ready to accept connections\" >&2" | ||
| , "trap 'exit 0' INT TERM" | ||
| , "while true; do" | ||
| , " sleep 0.1" | ||
| , "done" | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertions only check that the logged
psqlinvocation containsIHPSchema.sql, but the stubpsqlScriptdoesn’t validate that the-fpath actually exists (or that it was resolved viaIHP_LIB). This can let the regression test pass even ifgetDataFileNamestill returns the default relative path and the schema file can’t be found. Consider making the stubpsqlfail when the-ftarget doesn’t exist, or assert the exact expected resolved path fromIHP_LIBincommands.log.