namedtempfile.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #![deny(rust_2018_idioms)]
  2. use std::env;
  3. use std::ffi::{OsStr, OsString};
  4. use std::fs::File;
  5. use std::io::{Read, Seek, SeekFrom, Write};
  6. use std::path::{Path, PathBuf};
  7. use tempfile::{tempdir, Builder, NamedTempFile, TempPath};
  8. fn exists<P: AsRef<Path>>(path: P) -> bool {
  9. std::fs::metadata(path.as_ref()).is_ok()
  10. }
  11. #[test]
  12. fn test_basic() {
  13. let mut tmpfile = NamedTempFile::new().unwrap();
  14. write!(tmpfile, "abcde").unwrap();
  15. tmpfile.seek(SeekFrom::Start(0)).unwrap();
  16. let mut buf = String::new();
  17. tmpfile.read_to_string(&mut buf).unwrap();
  18. assert_eq!("abcde", buf);
  19. }
  20. #[test]
  21. fn test_deleted() {
  22. let tmpfile = NamedTempFile::new().unwrap();
  23. let path = tmpfile.path().to_path_buf();
  24. assert!(exists(&path));
  25. drop(tmpfile);
  26. assert!(!exists(&path));
  27. }
  28. #[test]
  29. fn test_persist() {
  30. let mut tmpfile = NamedTempFile::new().unwrap();
  31. let old_path = tmpfile.path().to_path_buf();
  32. let persist_path = env::temp_dir().join("persisted_temporary_file");
  33. write!(tmpfile, "abcde").unwrap();
  34. {
  35. assert!(exists(&old_path));
  36. let mut f = tmpfile.persist(&persist_path).unwrap();
  37. assert!(!exists(&old_path));
  38. // Check original file
  39. f.seek(SeekFrom::Start(0)).unwrap();
  40. let mut buf = String::new();
  41. f.read_to_string(&mut buf).unwrap();
  42. assert_eq!("abcde", buf);
  43. }
  44. {
  45. // Try opening it at the new path.
  46. let mut f = File::open(&persist_path).unwrap();
  47. f.seek(SeekFrom::Start(0)).unwrap();
  48. let mut buf = String::new();
  49. f.read_to_string(&mut buf).unwrap();
  50. assert_eq!("abcde", buf);
  51. }
  52. std::fs::remove_file(&persist_path).unwrap();
  53. }
  54. #[test]
  55. fn test_persist_noclobber() {
  56. let mut tmpfile = NamedTempFile::new().unwrap();
  57. let old_path = tmpfile.path().to_path_buf();
  58. let persist_target = NamedTempFile::new().unwrap();
  59. let persist_path = persist_target.path().to_path_buf();
  60. write!(tmpfile, "abcde").unwrap();
  61. assert!(exists(&old_path));
  62. {
  63. tmpfile = tmpfile.persist_noclobber(&persist_path).unwrap_err().into();
  64. assert!(exists(&old_path));
  65. std::fs::remove_file(&persist_path).unwrap();
  66. drop(persist_target);
  67. }
  68. tmpfile.persist_noclobber(&persist_path).unwrap();
  69. // Try opening it at the new path.
  70. let mut f = File::open(&persist_path).unwrap();
  71. f.seek(SeekFrom::Start(0)).unwrap();
  72. let mut buf = String::new();
  73. f.read_to_string(&mut buf).unwrap();
  74. assert_eq!("abcde", buf);
  75. std::fs::remove_file(&persist_path).unwrap();
  76. }
  77. #[test]
  78. fn test_customnamed() {
  79. let tmpfile = Builder::new()
  80. .prefix("tmp")
  81. .suffix(&".rs".to_string())
  82. .rand_bytes(12)
  83. .tempfile()
  84. .unwrap();
  85. let name = tmpfile.path().file_name().unwrap().to_str().unwrap();
  86. assert!(name.starts_with("tmp"));
  87. assert!(name.ends_with(".rs"));
  88. assert_eq!(name.len(), 18);
  89. }
  90. #[test]
  91. fn test_append() {
  92. let mut tmpfile = Builder::new().append(true).tempfile().unwrap();
  93. tmpfile.write(b"a").unwrap();
  94. tmpfile.seek(SeekFrom::Start(0)).unwrap();
  95. tmpfile.write(b"b").unwrap();
  96. tmpfile.seek(SeekFrom::Start(0)).unwrap();
  97. let mut buf = vec![0u8; 1];
  98. tmpfile.read_exact(&mut buf).unwrap();
  99. assert_eq!(buf, b"a");
  100. }
  101. #[test]
  102. fn test_reopen() {
  103. let source = NamedTempFile::new().unwrap();
  104. let mut first = source.reopen().unwrap();
  105. let mut second = source.reopen().unwrap();
  106. drop(source);
  107. write!(first, "abcde").expect("write failed");
  108. let mut buf = String::new();
  109. second.read_to_string(&mut buf).unwrap();
  110. assert_eq!("abcde", buf);
  111. }
  112. #[test]
  113. fn test_into_file() {
  114. let mut file = NamedTempFile::new().unwrap();
  115. let path = file.path().to_owned();
  116. write!(file, "abcde").expect("write failed");
  117. assert!(path.exists());
  118. let mut file = file.into_file();
  119. assert!(!path.exists());
  120. file.seek(SeekFrom::Start(0)).unwrap();
  121. let mut buf = String::new();
  122. file.read_to_string(&mut buf).unwrap();
  123. assert_eq!("abcde", buf);
  124. }
  125. #[test]
  126. fn test_immut() {
  127. let tmpfile = NamedTempFile::new().unwrap();
  128. (&tmpfile).write_all(b"abcde").unwrap();
  129. (&tmpfile).seek(SeekFrom::Start(0)).unwrap();
  130. let mut buf = String::new();
  131. (&tmpfile).read_to_string(&mut buf).unwrap();
  132. assert_eq!("abcde", buf);
  133. }
  134. #[test]
  135. fn test_temppath() {
  136. let mut tmpfile = NamedTempFile::new().unwrap();
  137. write!(tmpfile, "abcde").unwrap();
  138. let path = tmpfile.into_temp_path();
  139. assert!(path.is_file());
  140. }
  141. #[test]
  142. fn test_temppath_persist() {
  143. let mut tmpfile = NamedTempFile::new().unwrap();
  144. write!(tmpfile, "abcde").unwrap();
  145. let tmppath = tmpfile.into_temp_path();
  146. let old_path = tmppath.to_path_buf();
  147. let persist_path = env::temp_dir().join("persisted_temppath_file");
  148. {
  149. assert!(exists(&old_path));
  150. tmppath.persist(&persist_path).unwrap();
  151. assert!(!exists(&old_path));
  152. }
  153. {
  154. // Try opening it at the new path.
  155. let mut f = File::open(&persist_path).unwrap();
  156. f.seek(SeekFrom::Start(0)).unwrap();
  157. let mut buf = String::new();
  158. f.read_to_string(&mut buf).unwrap();
  159. assert_eq!("abcde", buf);
  160. }
  161. std::fs::remove_file(&persist_path).unwrap();
  162. }
  163. #[test]
  164. fn test_temppath_persist_noclobber() {
  165. let mut tmpfile = NamedTempFile::new().unwrap();
  166. write!(tmpfile, "abcde").unwrap();
  167. let mut tmppath = tmpfile.into_temp_path();
  168. let old_path = tmppath.to_path_buf();
  169. let persist_target = NamedTempFile::new().unwrap();
  170. let persist_path = persist_target.path().to_path_buf();
  171. assert!(exists(&old_path));
  172. {
  173. tmppath = tmppath.persist_noclobber(&persist_path).unwrap_err().into();
  174. assert!(exists(&old_path));
  175. std::fs::remove_file(&persist_path).unwrap();
  176. drop(persist_target);
  177. }
  178. tmppath.persist_noclobber(&persist_path).unwrap();
  179. // Try opening it at the new path.
  180. let mut f = File::open(&persist_path).unwrap();
  181. f.seek(SeekFrom::Start(0)).unwrap();
  182. let mut buf = String::new();
  183. f.read_to_string(&mut buf).unwrap();
  184. assert_eq!("abcde", buf);
  185. std::fs::remove_file(&persist_path).unwrap();
  186. }
  187. #[test]
  188. fn temp_path_from_existing() {
  189. let tmp_dir = tempdir().unwrap();
  190. let tmp_file_path_1 = tmp_dir.path().join("testfile1");
  191. let tmp_file_path_2 = tmp_dir.path().join("testfile2");
  192. File::create(&tmp_file_path_1).unwrap();
  193. assert!(tmp_file_path_1.exists(), "Test file 1 hasn't been created");
  194. File::create(&tmp_file_path_2).unwrap();
  195. assert!(tmp_file_path_2.exists(), "Test file 2 hasn't been created");
  196. let tmp_path = TempPath::from_path(&tmp_file_path_1);
  197. assert!(
  198. tmp_file_path_1.exists(),
  199. "Test file has been deleted before dropping TempPath"
  200. );
  201. drop(tmp_path);
  202. assert!(
  203. !tmp_file_path_1.exists(),
  204. "Test file exists after dropping TempPath"
  205. );
  206. assert!(
  207. tmp_file_path_2.exists(),
  208. "Test file 2 has been deleted before dropping TempDir"
  209. );
  210. }
  211. #[test]
  212. #[allow(unreachable_code)]
  213. fn temp_path_from_argument_types() {
  214. // This just has to compile
  215. return;
  216. TempPath::from_path("");
  217. TempPath::from_path(String::new());
  218. TempPath::from_path(OsStr::new(""));
  219. TempPath::from_path(OsString::new());
  220. TempPath::from_path(Path::new(""));
  221. TempPath::from_path(PathBuf::new());
  222. TempPath::from_path(PathBuf::new().into_boxed_path());
  223. }
  224. #[test]
  225. fn test_write_after_close() {
  226. let path = NamedTempFile::new().unwrap().into_temp_path();
  227. File::create(path).unwrap().write_all(b"test").unwrap();
  228. }
  229. #[test]
  230. fn test_change_dir() {
  231. env::set_current_dir(env::temp_dir()).unwrap();
  232. let tmpfile = NamedTempFile::new_in(".").unwrap();
  233. let path = env::current_dir().unwrap().join(tmpfile.path());
  234. env::set_current_dir("/").unwrap();
  235. drop(tmpfile);
  236. assert!(!exists(path))
  237. }
  238. #[test]
  239. fn test_into_parts() {
  240. let mut file = NamedTempFile::new().unwrap();
  241. write!(file, "abcd").expect("write failed");
  242. let (mut file, temp_path) = file.into_parts();
  243. let path = temp_path.to_path_buf();
  244. assert!(path.exists());
  245. drop(temp_path);
  246. assert!(!path.exists());
  247. write!(file, "efgh").expect("write failed");
  248. file.seek(SeekFrom::Start(0)).unwrap();
  249. let mut buf = String::new();
  250. file.read_to_string(&mut buf).unwrap();
  251. assert_eq!("abcdefgh", buf);
  252. }
  253. #[test]
  254. fn test_keep() {
  255. let mut tmpfile = NamedTempFile::new().unwrap();
  256. write!(tmpfile, "abcde").unwrap();
  257. let (mut f, temp_path) = tmpfile.into_parts();
  258. let path;
  259. {
  260. assert!(exists(&temp_path));
  261. path = temp_path.keep().unwrap();
  262. assert!(exists(&path));
  263. // Check original file
  264. f.seek(SeekFrom::Start(0)).unwrap();
  265. let mut buf = String::new();
  266. f.read_to_string(&mut buf).unwrap();
  267. assert_eq!("abcde", buf);
  268. }
  269. {
  270. // Try opening it again.
  271. let mut f = File::open(&path).unwrap();
  272. f.seek(SeekFrom::Start(0)).unwrap();
  273. let mut buf = String::new();
  274. f.read_to_string(&mut buf).unwrap();
  275. assert_eq!("abcde", buf);
  276. }
  277. std::fs::remove_file(&path).unwrap();
  278. }