⭐️ A friendly language for building type-safe, scalable systems!
0

Configure Feed

Select the types of activity you want to include in your feed.

gleam / compiler-cli / src / fs / tests.rs
7.2 kB 255 lines
1// SPDX-License-Identifier: Apache-2.0 2// SPDX-FileCopyrightText: 2022 The Gleam contributors 3 4use camino::{Utf8Path, Utf8PathBuf}; 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} 193 194#[test] 195fn hardlink_dir_copies_files_and_directories() { 196 let tmp = tempfile::tempdir().unwrap(); 197 let base = Utf8PathBuf::from_path_buf(tmp.path().to_path_buf()).unwrap(); 198 let src = base.join("src"); 199 let dest = base.join("dest"); 200 201 super::mkdir(&src).unwrap(); 202 super::mkdir(&src.join("sub")).unwrap(); 203 super::write(&src.join("a.txt"), "hello").unwrap(); 204 super::write(&src.join("sub").join("b.txt"), "world").unwrap(); 205 206 super::mkdir(&dest).unwrap(); 207 super::hardlink_dir(&src, &dest).unwrap(); 208 209 assert_eq!( 210 std::fs::read_to_string(dest.join("a.txt")).unwrap(), 211 "hello" 212 ); 213 assert_eq!( 214 std::fs::read_to_string(dest.join("sub").join("b.txt")).unwrap(), 215 "world" 216 ); 217} 218 219#[test] 220fn hardlink_dir_empty_source_creates_empty_dest() { 221 let tmp = tempfile::tempdir().unwrap(); 222 let base = Utf8PathBuf::from_path_buf(tmp.path().to_path_buf()).unwrap(); 223 let src = base.join("src"); 224 let dest = base.join("dest"); 225 226 super::mkdir(&src).unwrap(); 227 super::mkdir(&dest).unwrap(); 228 super::hardlink_dir(&src, &dest).unwrap(); 229 230 assert!(dest.exists()); 231 assert_eq!(std::fs::read_dir(&dest).unwrap().count(), 0); 232} 233 234#[cfg(unix)] 235#[test] 236fn hardlink_dir_skips_symlinks() { 237 let tmp = tempfile::tempdir().unwrap(); 238 let base = Utf8PathBuf::from_path_buf(tmp.path().to_path_buf()).unwrap(); 239 let src = base.join("src"); 240 let dest = base.join("dest"); 241 let outside = base.join("outside"); 242 243 super::mkdir(&src).unwrap(); 244 super::mkdir(&dest).unwrap(); 245 super::mkdir(&outside).unwrap(); 246 super::write(&src.join("real.txt"), "kept").unwrap(); 247 super::write(&outside.join("secret.txt"), "stolen").unwrap(); 248 249 std::os::unix::fs::symlink(outside.as_std_path(), src.join("escape").as_std_path()).unwrap(); 250 251 super::hardlink_dir(&src, &dest).unwrap(); 252 253 assert!(dest.join("real.txt").exists()); 254 assert!(!dest.join("escape").exists()); 255}