| 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 |
|
| 15 |
#[cfg(unix)] |
| 16 |
use testutils::TestResult; |
| 17 |
|
| 18 |
use crate::common::TestEnvironment; |
| 19 |
|
| 20 |
#[test] |
| 21 |
fn test_show() { |
| 22 |
let test_env = TestEnvironment::default(); |
| 23 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 24 |
let work_dir = test_env.work_dir("repo"); |
| 25 |
|
| 26 |
work_dir.write_file("file1", "a\n"); |
| 27 |
work_dir.run_jj(["new"]).success(); |
| 28 |
work_dir.write_file("file1", "b\n"); |
| 29 |
work_dir.create_dir("dir"); |
| 30 |
work_dir.write_file("dir/file2", "c\n"); |
| 31 |
work_dir.write_file("file3", "d\n"); |
| 32 |
|
| 33 |
// Can print the contents of a file in a commit |
| 34 |
let output = work_dir.run_jj(["file", "show", "file1", "-r", "@-"]); |
| 35 |
insta::assert_snapshot!(output, @" |
| 36 |
a |
| 37 |
[EOF] |
| 38 |
"); |
| 39 |
|
| 40 |
// Defaults to printing the working-copy version |
| 41 |
let output = work_dir.run_jj(["file", "show", "file1"]); |
| 42 |
insta::assert_snapshot!(output, @" |
| 43 |
b |
| 44 |
[EOF] |
| 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 output = work_dir.run_jj(["file", "show", subdir_file]); |
| 54 |
insta::assert_snapshot!(output, @" |
| 55 |
c |
| 56 |
[EOF] |
| 57 |
"); |
| 58 |
|
| 59 |
// Error if the path doesn't exist |
| 60 |
let output = work_dir.run_jj(["file", "show", "nonexistent"]); |
| 61 |
insta::assert_snapshot!(output, @" |
| 62 |
------- stderr ------- |
| 63 |
Error: No such path: nonexistent |
| 64 |
[EOF] |
| 65 |
[exit status: 1] |
| 66 |
"); |
| 67 |
|
| 68 |
// Can print files under the specified directory |
| 69 |
let output = work_dir.run_jj(["file", "show", "dir"]); |
| 70 |
insta::assert_snapshot!(output, @" |
| 71 |
c |
| 72 |
[EOF] |
| 73 |
"); |
| 74 |
|
| 75 |
// Can print a single file with template |
| 76 |
let template = r#""--- " ++ path ++ "\n""#; |
| 77 |
let output = work_dir.run_jj(["file", "show", "-T", template, "file1"]); |
| 78 |
insta::assert_snapshot!(output, @" |
| 79 |
--- file1 |
| 80 |
b |
| 81 |
[EOF] |
| 82 |
"); |
| 83 |
|
| 84 |
// Can print multiple files with template |
| 85 |
let output = work_dir.run_jj(["file", "show", "-T", template, "."]); |
| 86 |
insta::assert_snapshot!(output, @" |
| 87 |
--- dir/file2 |
| 88 |
c |
| 89 |
--- file1 |
| 90 |
b |
| 91 |
--- file3 |
| 92 |
d |
| 93 |
[EOF] |
| 94 |
"); |
| 95 |
|
| 96 |
// Can glob for multiple files too |
| 97 |
let output = work_dir.run_jj(["file", "show", "-T", template, "file*"]); |
| 98 |
insta::assert_snapshot!(output, @" |
| 99 |
--- file1 |
| 100 |
b |
| 101 |
--- file3 |
| 102 |
d |
| 103 |
[EOF] |
| 104 |
"); |
| 105 |
|
| 106 |
// Unmatched paths should generate warnings |
| 107 |
let output = work_dir.run_jj(["file", "show", "file1", "non-existent"]); |
| 108 |
insta::assert_snapshot!(output, @" |
| 109 |
b |
| 110 |
[EOF] |
| 111 |
------- stderr ------- |
| 112 |
Warning: No matching entries for paths: non-existent |
| 113 |
[EOF] |
| 114 |
"); |
| 115 |
|
| 116 |
// Can print a conflict |
| 117 |
work_dir.run_jj(["new"]).success(); |
| 118 |
work_dir.write_file("file1", "c\n"); |
| 119 |
work_dir |
| 120 |
.run_jj(["rebase", "-r", "@", "-o", "@--"]) |
| 121 |
.success(); |
| 122 |
let output = work_dir.run_jj(["file", "show", "file1"]); |
| 123 |
insta::assert_snapshot!(output, @r" |
| 124 |
<<<<<<< conflict 1 of 1 |
| 125 |
%%%%%%% diff from: rlvkpnrz fc7b369e (parents of rebased revision) |
| 126 |
\\\\\\\ to: qpvuntsm eb7b8a1f (rebase destination) |
| 127 |
-b |
| 128 |
+a |
| 129 |
+++++++ kmkuslsw f74f80c5 (rebased revision) |
| 130 |
c |
| 131 |
>>>>>>> conflict 1 of 1 ends |
| 132 |
[EOF] |
| 133 |
"); |
| 134 |
} |
| 135 |
|
| 136 |
#[cfg(unix)] |
| 137 |
#[test] |
| 138 |
fn test_show_symlink() -> TestResult { |
| 139 |
let test_env = TestEnvironment::default(); |
| 140 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 141 |
let work_dir = test_env.work_dir("repo"); |
| 142 |
|
| 143 |
work_dir.write_file("file1", "a\n"); |
| 144 |
work_dir.create_dir("dir"); |
| 145 |
work_dir.write_file("dir/file2", "c\n"); |
| 146 |
std::os::unix::fs::symlink("symlink1_target", work_dir.root().join("symlink1"))?; |
| 147 |
|
| 148 |
// Can print multiple files with template |
| 149 |
let template = r#""--- " ++ path ++ " [" ++ file_type ++ "]\n""#; |
| 150 |
let output = work_dir.run_jj(["file", "show", "-T", template, "."]); |
| 151 |
insta::assert_snapshot!(output, @r" |
| 152 |
--- dir/file2 [file] |
| 153 |
c |
| 154 |
--- file1 [file] |
| 155 |
a |
| 156 |
--- symlink1 [symlink] |
| 157 |
[EOF] |
| 158 |
------- stderr ------- |
| 159 |
Warning: Path 'symlink1' exists but is not a file. |
| 160 |
[EOF] |
| 161 |
"); |
| 162 |
Ok(()) |
| 163 |
} |