just playing with tangled
1// Copyright 2022 The Jujutsu Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::common::TestEnvironment;
16
17#[test]
18fn test_cat() {
19 let test_env = TestEnvironment::default();
20 test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
21 let repo_path = test_env.env_root().join("repo");
22
23 std::fs::write(repo_path.join("file1"), "a\n").unwrap();
24 test_env.jj_cmd_ok(&repo_path, &["new"]);
25 std::fs::write(repo_path.join("file1"), "b\n").unwrap();
26 std::fs::create_dir(repo_path.join("dir")).unwrap();
27 std::fs::write(repo_path.join("dir").join("file2"), "c\n").unwrap();
28
29 // Can print the contents of a file in a commit
30 let stdout = test_env.jj_cmd_success(&repo_path, &["cat", "file1", "-r", "@-"]);
31 insta::assert_snapshot!(stdout, @r###"
32 a
33 "###);
34
35 // Defaults to printing the working-copy version
36 let stdout = test_env.jj_cmd_success(&repo_path, &["cat", "file1"]);
37 insta::assert_snapshot!(stdout, @r###"
38 b
39 "###);
40
41 // `print` is an alias for `cat`
42 let stdout = test_env.jj_cmd_success(&repo_path, &["print", "file1"]);
43 insta::assert_snapshot!(stdout, @r###"
44 b
45 "###);
46
47 // Can print a file in a subdirectory
48 let subdir_file = if cfg!(unix) {
49 "dir/file2"
50 } else {
51 "dir\\file2"
52 };
53 let stdout = test_env.jj_cmd_success(&repo_path, &["cat", subdir_file]);
54 insta::assert_snapshot!(stdout, @r###"
55 c
56 "###);
57
58 // Error if the path doesn't exist
59 let stderr = test_env.jj_cmd_failure(&repo_path, &["cat", "nonexistent"]);
60 insta::assert_snapshot!(stderr, @r###"
61 Error: No such path: nonexistent
62 "###);
63
64 // Can print files under the specified directory
65 let stdout = test_env.jj_cmd_success(&repo_path, &["cat", "dir"]);
66 insta::assert_snapshot!(stdout, @r###"
67 c
68 "###);
69
70 // Can print multiple files
71 let stdout = test_env.jj_cmd_success(&repo_path, &["cat", "."]);
72 insta::assert_snapshot!(stdout, @r###"
73 c
74 b
75 "###);
76
77 // Unmatched paths should generate warnings
78 let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["cat", "file1", "non-existent"]);
79 insta::assert_snapshot!(stdout, @r###"
80 b
81 "###);
82 insta::assert_snapshot!(stderr, @r###"
83 Warning: No matching entries for paths: non-existent
84 "###);
85
86 // Can print a conflict
87 test_env.jj_cmd_ok(&repo_path, &["new"]);
88 std::fs::write(repo_path.join("file1"), "c\n").unwrap();
89 test_env.jj_cmd_ok(&repo_path, &["rebase", "-r", "@", "-d", "@--"]);
90 let stdout = test_env.jj_cmd_success(&repo_path, &["cat", "file1"]);
91 insta::assert_snapshot!(stdout, @r###"
92 <<<<<<< Conflict 1 of 1
93 %%%%%%% Changes from base to side #1
94 -b
95 +a
96 +++++++ Contents of side #2
97 c
98 >>>>>>>
99 "###);
100}
101
102#[cfg(unix)]
103#[test]
104fn test_cat_symlink() {
105 let test_env = TestEnvironment::default();
106 test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
107 let repo_path = test_env.env_root().join("repo");
108
109 std::fs::write(repo_path.join("file1"), "a\n").unwrap();
110 std::fs::create_dir(repo_path.join("dir")).unwrap();
111 std::fs::write(repo_path.join("dir").join("file2"), "c\n").unwrap();
112 std::os::unix::fs::symlink("symlink1_target", repo_path.join("symlink1")).unwrap();
113
114 // Can print multiple files
115 let (stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["cat", "."]);
116 insta::assert_snapshot!(stdout, @r###"
117 c
118 a
119 "###);
120 insta::assert_snapshot!(stderr, @r###"
121 Warning: Path exists but is not a file: symlink1
122 "###);
123}