Skip to content

Commit b8bb5ae

Browse files
authored
Simplify test project (#410)
1 parent ac80139 commit b8bb5ae

30 files changed

Lines changed: 210 additions & 212 deletions

nix/packages/checks/haskell-project.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mkCheck {
1515
export HOME="$TMPDIR"
1616
1717
for VERSION in $GHC_VERSIONS; do
18-
make test GHC="ghc-$VERSION"
18+
cabal build --with-compiler="ghc-$VERSION"
1919
done
2020
'';
2121

src/ghci/parse/show_targets.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ mod tests {
3434
#[test]
3535
fn test_parse_show_targets() {
3636
let show_paths = ShowPaths {
37-
cwd: NormalPath::from_cwd("tests/data/simple")
37+
cwd: NormalPath::from_cwd("tests/data/with-test-suite")
3838
.unwrap()
3939
.absolute()
4040
.to_owned(),
@@ -51,7 +51,6 @@ mod tests {
5151
src/MyLib.hs
5252
TestMain
5353
MyLib
54-
MyModule
5554
"
5655
)
5756
)
@@ -62,7 +61,6 @@ mod tests {
6261
LoadedModule::new(normal_path("src/MyLib.hs")),
6362
LoadedModule::with_name(normal_path("test/TestMain.hs"), "TestMain".to_owned()),
6463
LoadedModule::with_name(normal_path("src/MyLib.hs"), "MyLib".to_owned()),
65-
LoadedModule::with_name(normal_path("src/MyModule.hs"), "MyModule".to_owned()),
6664
]
6765
.into_iter()
6866
.collect()

test-harness/src/ghciwatch.rs

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ pub(crate) const LOG_FILENAME: &str = "ghciwatch.json";
3434
/// Builder for [`GhciWatch`].
3535
pub struct GhciWatchBuilder {
3636
project_directory: PathBuf,
37-
copy_extra: Vec<PathBuf>,
3837
ghciwatch_args: Vec<OsString>,
39-
make_args: Vec<String>,
4038
ghc_args: Vec<String>,
4139
cabal_args: Vec<String>,
40+
cabal_target: String,
4241
#[allow(clippy::type_complexity)]
4342
before_start: Option<Box<dyn FnOnce(PathBuf) -> BoxFuture<'static, miette::Result<()>> + Send>>,
4443
default_timeout: Duration,
@@ -51,12 +50,10 @@ impl GhciWatchBuilder {
5150
pub fn new(project_directory: impl AsRef<Path>) -> Self {
5251
Self {
5352
project_directory: project_directory.as_ref().to_owned(),
54-
// Gonna need this for the `Makefile` in `tests/data/simple`.
55-
copy_extra: vec!["tests/data/common".into()],
5653
ghciwatch_args: Default::default(),
5754
ghc_args: Default::default(),
58-
make_args: Default::default(),
5955
cabal_args: Default::default(),
56+
cabal_target: "my-simple-package".into(),
6057
before_start: None,
6158
default_timeout: Duration::from_secs(10),
6259
startup_timeout: Duration::from_secs(60),
@@ -90,19 +87,6 @@ impl GhciWatchBuilder {
9087
self
9188
}
9289

93-
/// Add an argument to the `make` invocations.
94-
pub fn with_make_arg(mut self, arg: impl AsRef<str>) -> Self {
95-
self.make_args.push(arg.as_ref().to_owned());
96-
self
97-
}
98-
99-
/// Add multiple arguments to the `make` invocations.
100-
pub fn with_make_args(mut self, args: impl IntoIterator<Item = impl AsRef<str>>) -> Self {
101-
self.make_args
102-
.extend(args.into_iter().map(|s| s.as_ref().to_owned()));
103-
self
104-
}
105-
10690
/// Add an argument to the `cabal` invocations.
10791
pub fn with_cabal_arg(mut self, arg: impl AsRef<str>) -> Self {
10892
self.cabal_args.push(arg.as_ref().to_owned());
@@ -116,6 +100,12 @@ impl GhciWatchBuilder {
116100
self
117101
}
118102

103+
/// Set the Cabal target to open a `cabal v2-repl` for.
104+
pub fn with_cabal_target(mut self, target: impl AsRef<str>) -> Self {
105+
self.cabal_target = target.as_ref().to_owned();
106+
self
107+
}
108+
119109
/// Add a hook to run after project files are copied to the temporary directory but before
120110
/// `ghciwatch` is started.
121111
pub fn before_start<F>(mut self, before_start: impl Fn(PathBuf) -> F + Send + 'static) -> Self
@@ -164,14 +154,6 @@ impl GhciWatchBuilder {
164154
.extend(log_filters.into_iter().map(|s| s.as_ref().to_owned()));
165155
self
166156
}
167-
168-
/// Copy an extra directory or path to the temporary directory where tests are run.
169-
///
170-
/// This will be copied as a sibling of the project directory.
171-
pub fn copy_extra(mut self, path: impl AsRef<Path>) -> Self {
172-
self.copy_extra.push(path.as_ref().to_owned());
173-
self
174-
}
175157
}
176158

177159
struct Session {
@@ -269,8 +251,10 @@ impl GhciWatch {
269251
write_cabal_config(&fs, &tempdir).await?;
270252
check_ghc_version(&tempdir, &ghc_version).await?;
271253

272-
let mut paths_to_copy = vec![&builder.project_directory];
273-
paths_to_copy.extend(builder.copy_extra.iter());
254+
let inner_tempdir = tempdir.join("tmp");
255+
fs.create_dir(&inner_tempdir).await?;
256+
257+
let paths_to_copy = vec![&builder.project_directory];
274258
tracing::info!(?paths_to_copy, "Copying project files");
275259
fs_extra::copy_items(&paths_to_copy, &tempdir, &Default::default())
276260
.into_diagnostic()
@@ -293,17 +277,16 @@ impl GhciWatch {
293277
let log_path = tempdir.join(LOG_FILENAME);
294278

295279
tracing::info!("Starting ghciwatch");
296-
let repl_command = shell_words::join(
297-
[
298-
"make",
299-
"ghci",
300-
&format!("GHC=ghc-{ghc_version}"),
301-
&format!("EXTRA_GHC_OPTS={}", shell_words::join(builder.ghc_args)),
302-
&format!("CABAL_OPTS={}", shell_words::join(builder.cabal_args)),
303-
]
304-
.into_iter()
305-
.chain(builder.make_args.iter().map(|s| s.as_str())),
306-
);
280+
let mut repl_command = vec!["cabal".into(), format!("--with-compiler=ghc-{ghc_version}")];
281+
repl_command.extend(builder.cabal_args.iter().cloned());
282+
repl_command.push(format!(
283+
"--repl-options={}",
284+
shell_words::join(&builder.ghc_args)
285+
));
286+
repl_command.push("v2-repl".into());
287+
repl_command.push(builder.cabal_target.clone());
288+
289+
let repl_command = shell_words::join(repl_command);
307290

308291
let log_filters = ["ghciwatch::watcher=trace", "ghciwatch=debug"]
309292
.into_iter()
@@ -319,11 +302,7 @@ impl GhciWatch {
319302
"--watch",
320303
"src",
321304
"--watch",
322-
"package.yaml",
323-
"--restart-glob",
324-
"**/package.yaml",
325-
"--before-startup-shell",
326-
"hpack --force .",
305+
"my-simple-package.cabal", // This is going to get me in trouble.
327306
"--log-filter",
328307
&log_filters,
329308
"--trace-spans",
@@ -334,6 +313,8 @@ impl GhciWatch {
334313
.args(builder.ghciwatch_args)
335314
.current_dir(&cwd)
336315
.env("HOME", &tempdir)
316+
.env("CABAL_DIR", tempdir.join(".cabal"))
317+
.env("TMPDIR", &inner_tempdir)
337318
// GHC will quote things with Unicode quotes unless we set this variable.
338319
// Very cute.
339320
// https://gitlab.haskell.org/ghc/ghc/-/blob/288235bbe5a59b8a1bda80aaacd59e5717417726/compiler/GHC/Driver/Session.hs#L1084-L1085

tests/all_good.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async fn can_detect_compilation_failure() {
1212
.start()
1313
.await
1414
.expect("ghciwatch starts");
15-
let module_path = session.path("src/MyModule.hs");
15+
let module_path = session.path("src/MyLib.hs");
1616

1717
session.wait_until_ready().await.expect("ghciwatch loads");
1818

tests/clear.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use test_harness::GhciWatchBuilder;
88
#[test]
99
async fn clears_on_reload_and_restart() {
1010
let mut session = GhciWatchBuilder::new("tests/data/simple")
11-
.with_arg("--clear")
11+
.with_args(["--clear", "--restart-glob", "**/*.cabal"])
1212
.with_log_filter("ghciwatch::ghci[clear]=trace")
1313
.start()
1414
.await
@@ -43,10 +43,10 @@ async fn clears_on_reload_and_restart() {
4343
.await
4444
.unwrap();
4545

46-
// Modify the `package.yaml` to trigger a restart.
46+
// Modify the `.cabal` file to trigger a restart.
4747
session
4848
.fs()
49-
.append(session.path("package.yaml"), "\n")
49+
.append(session.path("my-simple-package.cabal"), "\n")
5050
.await
5151
.unwrap();
5252

tests/data/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist-newstyle

tests/data/common/common.mk

Lines changed: 0 additions & 50 deletions
This file was deleted.

tests/data/simple/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/data/simple/Makefile

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cabal-version: 3.0
2+
3+
name: my-simple-package
4+
version: 0.1.0.0
5+
author: Rebecca Turner <rebeccat@mercury.com>
6+
build-type: Simple
7+
8+
library
9+
exposed-modules: MyLib
10+
hs-source-dirs: src
11+
ghc-options: -Wall
12+
build-depends: base
13+
default-language: Haskell2010

0 commit comments

Comments
 (0)