Fork of daniellemaywood.uk/gleam — Wasm codegen work
5.2 kB
192 lines
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Gleam contributors
3
4use camino::Utf8Path;
5use itertools::Itertools;
6
7#[test]
8fn is_inside_git_work_tree_ok() {
9 let tmp_dir = tempfile::tempdir().unwrap();
10 let path = Utf8Path::from_path(tmp_dir.path()).expect("Non Utf-8 Path");
11
12 assert!(!super::is_inside_git_work_tree(path).unwrap());
13 assert_eq!(super::git_init(path), Ok(()));
14 assert!(super::is_inside_git_work_tree(path).unwrap())
15}
16
17#[test]
18fn git_init_success() {
19 let tmp_dir = tempfile::tempdir().unwrap();
20 let path = Utf8Path::from_path(tmp_dir.path()).expect("Non Utf-8 Path");
21 let git = path.join(".git");
22
23 assert!(!git.exists());
24 assert_eq!(super::git_init(path), Ok(()));
25 assert!(git.exists());
26}
27
28#[test]
29fn git_init_already_in_git() {
30 let tmp_dir = tempfile::tempdir().unwrap();
31 let git = Utf8Path::from_path(tmp_dir.path())
32 .expect("Non Utf-8 Path")
33 .join(".git");
34 assert!(!git.exists());
35 assert_eq!(
36 super::git_init(Utf8Path::from_path(tmp_dir.path()).expect("Non Utf-8 Path")),
37 Ok(())
38 );
39 assert!(git.exists());
40
41 let sub = Utf8Path::from_path(tmp_dir.path())
42 .expect("Non Utf-8 Path")
43 .join("subproject");
44 let git = sub.join(".git");
45 crate::fs::mkdir(&sub).unwrap();
46 assert!(!git.exists());
47 assert_eq!(super::git_init(&sub), Ok(()));
48 assert!(!git.exists());
49}
50
51#[test]
52fn exclude_build_dir() {
53 /*
54 a
55 |-- gleam.toml
56 |-- build
57 | |-- f.gleam # do not count as gleam file
58 b
59 |-- build
60 | |-- f.gleam # count as gleam file
61 */
62
63 let tmp_dir = tempfile::tempdir().unwrap();
64 let path = Utf8Path::from_path(tmp_dir.path()).expect("Non Utf-8 Path");
65
66 // excluded gleam file
67 {
68 let gleam_toml = path.join("a/gleam.toml").to_path_buf();
69 super::write(&gleam_toml, "").unwrap();
70
71 let gleam_file = path.join("a/build/f.gleam").to_path_buf();
72 super::write(&gleam_file, "").unwrap();
73 };
74
75 // included gleam file
76 let gleam_file = path.join("b/build/f.gleam").to_path_buf();
77 super::write(&gleam_file, "").unwrap();
78
79 let files = super::gleam_files(path).collect::<Vec<_>>();
80
81 assert_eq!(files, vec![gleam_file]);
82}
83
84#[test]
85fn erlang_files_include_gitignored_files() {
86 let tmp_dir = tempfile::tempdir().unwrap();
87 let path = Utf8Path::from_path(tmp_dir.path()).expect("Non Utf-8 Path");
88
89 let included_files = &[
90 ".hidden.erl",
91 "abc.erl",
92 "abc.hrl",
93 "build/include/abc.erl",
94 "build/include/abc.hrl",
95 "ignored.erl",
96 "ignored.hrl",
97 ];
98
99 let excluded_files = &[
100 ".gitignore",
101 "abc.gleam",
102 "abc.js",
103 "build/abc.gleam",
104 "build/abc.js",
105 ];
106
107 let gitignore = "build/
108ignored.*";
109
110 for &file in included_files.iter().chain(excluded_files) {
111 let contents = match file {
112 ".gitignore" => gitignore,
113 _ => "",
114 };
115
116 super::write(&path.join(file), contents).unwrap();
117 }
118
119 let mut chosen_files = super::erlang_files(path).collect_vec();
120 chosen_files.sort_unstable();
121
122 let expected_files = included_files.iter().map(|s| path.join(s)).collect_vec();
123
124 assert_eq!(expected_files, chosen_files);
125}
126
127#[test]
128fn is_gleam_path_test() {
129 assert!(super::is_gleam_path(
130 Utf8Path::new("/some-prefix/a.gleam"),
131 Utf8Path::new("/some-prefix/")
132 ));
133
134 assert!(super::is_gleam_path(
135 Utf8Path::new("/some-prefix/one_two/a.gleam"),
136 Utf8Path::new("/some-prefix/")
137 ));
138
139 assert!(super::is_gleam_path(
140 Utf8Path::new("/some-prefix/one_two/a123.gleam"),
141 Utf8Path::new("/some-prefix/")
142 ));
143
144 assert!(super::is_gleam_path(
145 Utf8Path::new("/some-prefix/one_2/a123.gleam"),
146 Utf8Path::new("/some-prefix/")
147 ));
148}
149
150#[test]
151fn extract_distro_id_test() {
152 let os_release = "
153PRETTY_NAME=\"Debian GNU/Linux 12 (bookworm)\"
154NAME=\"Debian GNU/Linux\"
155VERSION_ID=\"12\"
156VERSION=\"12 (bookworm)\"
157VERSION_CODENAME=bookworm
158ID=debian
159HOME_URL=\"https://www.debian.org/\"
160";
161 assert_eq!(super::extract_distro_id(os_release.to_string()), "debian");
162
163 let os_release = "
164VERSION_CODENAME=jammy
165ID=ubuntu
166ID_LIKE=debian
167HOME_URL=\"https://www.ubuntu.com/\"
168";
169 assert_eq!(super::extract_distro_id(os_release.to_string()), "ubuntu");
170
171 assert_eq!(super::extract_distro_id("".to_string()), "");
172 assert_eq!(super::extract_distro_id("\n".to_string()), "");
173 assert_eq!(super::extract_distro_id("ID=".to_string()), "");
174 assert_eq!(super::extract_distro_id("ID= ".to_string()), " ");
175 assert_eq!(
176 super::extract_distro_id("ID= space test ".to_string()),
177 " space test "
178 );
179 assert_eq!(super::extract_distro_id("id=ubuntu".to_string()), "");
180 assert_eq!(
181 super::extract_distro_id("NAME=\"Debian\"\nID=debian".to_string()),
182 "debian"
183 );
184 assert_eq!(
185 super::extract_distro_id("\n\nNAME=\n\n\nID=test123\n".to_string()),
186 "test123"
187 );
188 assert_eq!(
189 super::extract_distro_id("\nID=\"id first\"\nID=another_id".to_string()),
190 "id first"
191 );
192}