Elffuss BraidHub

utopia-ia/elffuss-braid public

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