Elffuss BraidHub

utopia-ia/elffuss-braid public

main · 2.3 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! Writing a changeset into a plain directory: the files only, with the
5//! checkout's modes and its refusal to follow symlinks.
6
7#![cfg(feature = "materialize")]
8
9mod common;
10
11use braid_format::{ChangeOp, GitId};
12use braid_wc::{Error, materialize};
13use common::{Fixture, listing};
14
15#[test]
16fn a_changeset_is_written_with_its_files_only() {
17 let f = Fixture::new();
18 let id = f.commit(
19 &[],
20 vec![
21 ("bin/run", f.exec("#!/bin/sh\n")),
22 ("docs/a.md", f.file("a\n")),
23 ("link", f.symlink(b"docs/a.md")),
24 (
25 "sub",
26 ChangeOp::Gitlink {
27 commit: GitId::Sha1([7; 20]),
28 },
29 ),
30 ],
31 );
32 let dir = f.path("tree");
33 assert_eq!(materialize(f.repo.store(), id, &dir).unwrap(), 4);
34 assert_eq!(
35 listing(&dir),
36 ["bin", "bin/run", "docs", "docs/a.md", "link", "sub"]
37 );
38 assert_eq!(
39 std::fs::read_to_string(dir.join("docs/a.md")).unwrap(),
40 "a\n"
41 );
42 let link = std::fs::symlink_metadata(dir.join("link")).unwrap();
43 assert!(link.file_type().is_symlink());
44 assert_eq!(
45 std::fs::read_link(dir.join("link")).unwrap(),
46 std::path::Path::new("docs/a.md")
47 );
48 #[cfg(unix)]
49 {
50 use std::os::unix::fs::PermissionsExt;
51 let mode = |path: &str| {
52 std::fs::metadata(dir.join(path))
53 .unwrap()
54 .permissions()
55 .mode()
56 & 0o777
57 };
58 assert_eq!(mode("bin/run") & 0o111, 0o111);
59 assert_eq!(mode("docs/a.md") & 0o111, 0);
60 }
61}
62
63#[test]
64fn only_an_empty_or_missing_directory_is_written() {
65 let f = Fixture::new();
66 let id = f.commit(&[], vec![("a", f.file("a\n"))]);
67 let empty = f.path("empty");
68 std::fs::create_dir(&empty).unwrap();
69 assert_eq!(materialize(f.repo.store(), id, &empty).unwrap(), 1);
70 // Not into one that holds anything, such as what was just written.
71 assert!(matches!(
72 materialize(f.repo.store(), id, &empty),
73 Err(Error::DirectoryNotEmpty(_))
74 ));
75 // The directory's parent must exist.
76 assert!(materialize(f.repo.store(), id, &f.path("no/such")).is_err());
77}