Skip to content

Commit

Permalink
WIP: add tests for cabal status
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor committed Sep 21, 2022
1 parent ee7cd24 commit e5a68bc
Show file tree
Hide file tree
Showing 27 changed files with 388 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cabal-testsuite/PackageTests/Status/Invalid/InvalidDep.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cabal-version: 2.4
name: InvalidDep
version: 0.1.0.0

executable Inv
main-is: Main.hs
hs-source-dirs: src
build-depends: another-framework
default-language: Haskell2010

1 change: 1 addition & 0 deletions cabal-testsuite/PackageTests/Status/Invalid/cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages: ./
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: another-framework
version: 0.8.1.1
build-type: Simple
cabal-version: >= 1.10

library
build-depends: base <3 && >=3
default-language: Haskell2010
4 changes: 4 additions & 0 deletions cabal-testsuite/PackageTests/Status/Invalid/src/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Main where

main :: IO ()
main = putStr "Test"
20 changes: 20 additions & 0 deletions cabal-testsuite/PackageTests/Status/Invalid/unbuildabledep.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# cabal v2-update
Downloading the latest package list from test-local-repo
# cabal status
Resolving dependencies...
Error: cabal: Could not resolve dependencies:
[__0] trying: InvalidDep-0.1.0.0 (user goal)
[__1] trying: another-framework-0.8.1.1 (dependency of InvalidDep)
[__2] next goal: base (dependency of another-framework)
[__2] rejecting: base-<VERSION>/installed-<HASH> (conflict: another-framework => base<3 && >=3)
[__2] fail (backjumping, conflict set: another-framework, base)
After searching the rest of the dependency tree exhaustively, these were the goals I've had most trouble fulfilling: another-framework (3), InvalidDep (2), base (2)
# cabal status
Resolving dependencies...
Error: cabal: Could not resolve dependencies:
[__0] trying: InvalidDep-0.1.0.0 (user goal)
[__1] trying: another-framework-0.8.1.1 (dependency of InvalidDep)
[__2] next goal: base (dependency of another-framework)
[__2] rejecting: base-<VERSION>/installed-<HASH> (conflict: another-framework => base<3 && >=3)
[__2] fail (backjumping, conflict set: another-framework, base)
After searching the rest of the dependency tree exhaustively, these were the goals I've had most trouble fulfilling: another-framework (3), InvalidDep (2), base (2)
11 changes: 11 additions & 0 deletions cabal-testsuite/PackageTests/Status/Invalid/unbuildabledep.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{-# LANGUAGE OverloadedStrings #-}
import Test.Cabal.Prelude
import Test.Cabal.DecodeShowBuildInfo

main = cabalTest $ withRepo "repo" $ do
-- no build plan available
r <- fails $ cabal' "status" ["--output-format=json", "--target", "src/Main.hs"]
assertOutputContains "Could not resolve dependencies" r
-- TODO: should this actually work?
r <- fails $ cabal' "status" ["--output-format=json", "--compiler"]
assertOutputContains "Could not resolve dependencies" r
69 changes: 69 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/Simple.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
cabal-version: 2.4
name: Simple
version: 0.1.0.0

library
exposed-modules:
MyLib
MyLib2

build-depends: base
hs-source-dirs: src
default-language: Haskell2010

library unbuildable
exposed-modules: MyLib2
hs-source-dirs: src
build-depends: base
default-language: Haskell2010
buildable: False

library compilefail
exposed-modules: Fails
hs-source-dirs: src
build-depends: base
default-language: Haskell2010
buildable: False

executable Simple
main-is: Main.hs

-- Module that belongs to multiple components
other-modules: MyLib
hs-source-dirs: src exe
default-language: Haskell2010

-- Just some simple config to test 'exes' meta command
executable Simple2
main-is: Main2.hs

-- Module that belongs to multiple components
other-modules: MyLib
hs-source-dirs: src exe
default-language: Haskell2010

test-suite Tests
type: exitcode-stdio-1.0
main-is: Main.hs
hs-source-dirs: test
build-depends: base

benchmark Benchs
type: exitcode-stdio-1.0
main-is: Main.hs
hs-source-dirs: test
build-depends: base

foreign-library myforeignlib
type: native-shared

if os(windows)
options: standalone

other-modules: MyForeignLib.Hello
MyForeignLib.SomeBindings
MyForeignLib.AnotherVal
build-depends: base
hs-source-dirs: flibsrc
c-sources: csrc/MyForeignLibWrapper.c
default-language: Haskell2010
3 changes: 3 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/bench/Bench.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Bench where

main = putStr "Benchmarks!"
1 change: 1 addition & 0 deletions cabal-testsuite/PackageTests/Status/Simple/cabal.project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages: ./
3 changes: 3 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/compiler.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# cabal status
Resolving dependencies...
{"cabal-version":"3.9","compiler":{"flavour":"ghc","compiler-id":"ghc-<GHCVER>","path":"<GHCPATH>"}}
8 changes: 8 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/compiler.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Test.Cabal.Prelude
import Test.Cabal.DecodeShowBuildInfo
import Data.Maybe

main = cabalTest $ do
r <- cabal' "status" ["--output-format=json", "--compiler"]
statusInfo <- withJsonOutput r
assertBool "Must contain compiler information" (isJust $ siCompiler statusInfo)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdlib.h>
#include <stdbool.h>
#include "HsFFI.h"

bool myForeignLibInit(void){
int argc = 2;
char *argv[] = { "+RTS", "-A32m", NULL };
char **pargv = argv;

// Initialize Haskell runtime
hs_init(&argc, &pargv);

// do any other initialization here and
// return false if there was a problem
return true;
}

void myForeignLibExit(void){
hs_exit();
}

int cFoo2() {
return 1234;
}
4 changes: 4 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/exe/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Main where

main :: IO ()
main = putStrLn "Test"
4 changes: 4 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/exe/Main2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Main where

main :: IO ()
main = putStrLn "Test"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module MyForeignLib.AnotherVal where

anotherVal = 189
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- | Module with single foreign export
module MyForeignLib.Hello (sayHi) where

import MyForeignLib.SomeBindings
import MyForeignLib.AnotherVal

foreign export ccall sayHi :: IO ()

-- | Say hi!
sayHi :: IO ()
sayHi = putStrLn $
"Hi from a foreign library! Foo has value " ++ show valueOfFoo
++ " and anotherVal has value " ++ show anotherVal
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- | Module that needs the hsc2hs preprocessor
module MyForeignLib.SomeBindings where

#define FOO 1

#ifdef FOO
-- | Value guarded by a CPP flag
valueOfFoo :: Int
valueOfFoo = 5678
#endif
3 changes: 3 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/plan.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# cabal status
Resolving dependencies...
{"cabal-version":"3.9","targets":[{"target":"src/Main.hs","unit-id":null}]}
7 changes: 7 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/plan.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Test.Cabal.Prelude

main = cabalTest $ do
-- Make sure plan.json is generated, even if no target is resolved
cabal "status" ["--output-format=json", "--target", "src/Main.hs"]
withPlan $ do
pure ()
41 changes: 41 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/simple.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# cabal status
Error: cabal: The status command requires the flag '--output-format'.
# cabal status
Resolving dependencies...
{"cabal-version":"3.9","targets":[{"target":"exe/Main.hs","unit-id":"Simple-0.1.0.0-inplace-Simple"}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"exe/Main.hs","unit-id":"Simple-0.1.0.0-inplace-Simple"},{"target":"exe/Main2.hs","unit-id":"Simple-0.1.0.0-inplace-Simple2"}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"src/MyLib.hs","unit-id":null}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"src/MyLib2.hs","unit-id":null}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"bench/Bench.hs","unit-id":null}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"test/Main.hs","unit-id":null}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"flibsrc/MyForeignLib/AnotherVal.hs","unit-id":"Simple-0.1.0.0-inplace-myforeignlib"},{"target":"flibsrc/MyForeignLib/Hello.hs","unit-id":"Simple-0.1.0.0-inplace-myforeignlib"},{"target":"flibsrc/MyForeignLib/SomeBindings.hsc","unit-id":"Simple-0.1.0.0-inplace-myforeignlib"},{"target":"csrc/MyForeignLibWrapper.c","unit-id":null}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"Benchs","unit-id":"Simple-0.1.0.0-inplace-Benchs"}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"all","unit-id":"Simple-0.1.0.0-inplace"},{"target":"all","unit-id":"Simple-0.1.0.0-inplace-Simple"},{"target":"all","unit-id":"Simple-0.1.0.0-inplace-Simple2"},{"target":"all","unit-id":"Simple-0.1.0.0-inplace-myforeignlib"}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"exes","unit-id":"Simple-0.1.0.0-inplace-Simple"},{"target":"exes","unit-id":"Simple-0.1.0.0-inplace-Simple2"}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"tests","unit-id":"Simple-0.1.0.0-inplace-Tests"}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"benchmarks","unit-id":"Simple-0.1.0.0-inplace-Benchs"}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"executables","unit-id":"Simple-0.1.0.0-inplace-Simple"},{"target":"executables","unit-id":"Simple-0.1.0.0-inplace-Simple2"}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"Main2.hs","unit-id":null}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"Main3.hs","unit-id":null},{"target":"src/MyLib2.hs","unit-id":null},{"target":"Main3.hs","unit-id":null}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"Lib.hs","unit-id":null}]}
# cabal status
{"cabal-version":"3.9","compiler":{"flavour":"ghc","compiler-id":"ghc-<GHCVER>","path":"<GHCPATH>"},"targets":[{"target":"Lib.hs","unit-id":null}]}
# cabal status
{"cabal-version":"3.9","targets":[{"target":"Lib2.hs","unit-id":null}]}
# cabal status
{"cabal-version":"3.9","compiler":{"flavour":"ghc","compiler-id":"ghc-<GHCVER>","path":"<GHCPATH>"},"targets":[{"target":"Lib.hs","unit-id":null}]}
54 changes: 54 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/simple.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Test.Cabal.Prelude
import Test.Cabal.DecodeShowBuildInfo
import Data.Maybe (isJust)
import Data.List (sort, nub)

main = cabalTest $ do
-- output-format flag is missing but required, must fail
r <- fails $ cabal' "status" ["--target", "Main.hs"]
assertOutputContains "The status command requires the flag '--output-format'." r

-- Simple file target tests
runStatusWithTargets ["exe/Main.hs"]
runStatusWithTargets ["exe/Main.hs", "exe/Main2.hs"] -- multiple targets
runStatusWithTargets ["src/MyLib.hs"] -- belongs to multiple components
runStatusWithTargets ["src/MyLib2.hs"]
runStatusWithTargets ["bench/Bench.hs"]
runStatusWithTargets ["test/Main.hs"]
runStatusWithTargets ["flibsrc/MyForeignLib/AnotherVal.hs",
"flibsrc/MyForeignLib/Hello.hs",
"flibsrc/MyForeignLib/SomeBindings.hsc",
"csrc/MyForeignLibWrapper.c"
]
-- TODO: this should not fail, that's a bug!
-- runStatusWithTargets ["lib:Simple", "exe:Simple", "Simple:exe:Simple"]
-- pkgs syntax twsts
runStatusWithTargets ["Benchs"]

-- meta targets
runStatusWithTargets ["all"]
runStatusWithTargets ["exes"]
runStatusWithTargets ["tests"]
runStatusWithTargets ["benchmarks"]

-- unknown target selectors
runStatusWithTargets ["executables"]
runStatusWithTargets ["Main2.hs"]

-- partially works, Main3.hs isn't known while `src/MyLib2.hs` is.
runStatusWithTargets ["Main3.hs", "src/MyLib2.hs"]

-- component fails to compile, still works
cabal "status" ["--output-format=json", "--target", "Lib.hs"]
cabal "status" ["--output-format=json", "--compiler", "--target", "Lib.hs"]
-- unbuildable target, resolves to 'null'
cabal "status" ["--output-format=json", "--target", "Lib2.hs"]
cabal "status" ["--output-format=json", "--compiler", "--target", "Lib.hs"]
where
runStatusWithTargets :: [String] -> TestM ()
runStatusWithTargets targets = do
r <- cabal' "status" $ ["--output-format=json"] ++ concatMap (\t -> ["--target", t]) targets
statusInfo <- withJsonOutput r
assertBool "Must contain targets" (isJust $ siTargetResolving statusInfo)
assertBool "Must contain all targets at least once"
(Just (nub $ sort targets) == fmap (nub . sort . map rtOriginalTarget) (siTargetResolving statusInfo))
4 changes: 4 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/src/Fails.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Fails where

-- fails to compile intentionally
foo =
4 changes: 4 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/src/MyLib.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module MyLib (someFunc) where

someFunc :: IO ()
someFunc = putStrLn "someFunc"
4 changes: 4 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/src/MyLib2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module MyLib2 (someFunc) where

someFunc :: IO ()
someFunc = putStrLn "someFunc"
3 changes: 3 additions & 0 deletions cabal-testsuite/PackageTests/Status/Simple/test/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Bench where

main = putStr "Tests!"
Loading

0 comments on commit e5a68bc

Please sign in to comment.