Fork of daniellemaywood.uk/gleam — Wasm codegen work
3.3 kB
122 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2025 The Gleam contributors
3
4use std::time::SystemTime;
5
6use gleam_core::{Error, io::FileSystemWriter, paths::ProjectPaths};
7
8use crate::{files::FileSystemProxy, tests::Action};
9
10use super::LanguageServerTestIO;
11
12type Router = crate::router::Router<LanguageServerTestIO, LanguageServerTestIO>;
13
14#[test]
15fn recompile_after_no_changes_does_not_redownload_dependencies() {
16 let paths = ProjectPaths::new("/app".into());
17 let (io, mut router) = set_up_minimal_router(&paths);
18
19 assert_eq!(
20 compile(&mut router, &paths),
21 Ok(()),
22 "Pre-condition: Initial compile should succeed"
23 );
24
25 {
26 let mut actions = io.actions.lock().unwrap();
27 assert!(
28 actions.contains(&Action::DownloadDependencies),
29 "Expectation: Initial compile should download dependencies"
30 );
31 actions.clear();
32 }
33
34 assert_eq!(
35 compile(&mut router, &paths),
36 Ok(()),
37 "Recompile should succeed"
38 );
39
40 {
41 let actions = io.actions.lock().unwrap();
42 assert!(
43 !actions.contains(&Action::DownloadDependencies),
44 "Recompile should not re-download dependencies"
45 );
46 }
47}
48
49#[test]
50fn deleting_build_dir_redownloads_dependencies() {
51 let paths = ProjectPaths::new("/app".into());
52 let (io, mut router) = set_up_minimal_router(&paths);
53
54 _ = compile(&mut router, &paths);
55 io.actions.lock().unwrap().clear();
56
57 io.delete_directory(&paths.build_directory()).unwrap();
58 assert_eq!(
59 compile(&mut router, &paths),
60 Ok(()),
61 "Compile after deleting build directory should succeed"
62 );
63
64 {
65 let actions = io.actions.lock().unwrap();
66 assert!(
67 actions.contains(&Action::DownloadDependencies),
68 "Compile after deleting build directory should re-download dependencies"
69 );
70 }
71}
72
73#[test]
74fn changing_config_redownloads_dependencies() {
75 let paths = ProjectPaths::new("/app".into());
76 let (io, mut router) = set_up_minimal_router(&paths);
77
78 _ = compile(&mut router, &paths);
79 io.actions.lock().unwrap().clear();
80
81 let toml = r#"name = "wobble"
82 version = "1.0.0""#;
83 io.write(&paths.root_config(), toml).unwrap();
84 io.io
85 .try_set_modification_time(&paths.root_config(), SystemTime::now())
86 .unwrap();
87
88 assert_eq!(
89 compile(&mut router, &paths),
90 Ok(()),
91 "Compile after changing gleam.toml should succeed"
92 );
93
94 {
95 let actions = io.actions.lock().unwrap();
96 assert!(
97 actions.contains(&Action::DownloadDependencies),
98 "Compile after changing gleam.toml should re-download dependencies"
99 );
100 }
101}
102
103fn compile(router: &mut Router, paths: &ProjectPaths) -> Result<(), Error> {
104 router
105 .project_for_path(paths.root().into())
106 .unwrap()
107 .unwrap()
108 .engine
109 .compile_please()
110 .result
111}
112
113fn set_up_minimal_router(paths: &ProjectPaths) -> (LanguageServerTestIO, Router) {
114 let io = LanguageServerTestIO::new();
115 let router = Router::new(io.clone(), FileSystemProxy::new(io.clone()));
116
117 let toml = r#"name = "wibble"
118 version = "1.0.0""#;
119
120 io.write(&paths.root_config(), toml).unwrap();
121 (io, router)
122}