1 use crate::common::*;
2 
3 const JUSTFILE: &str = r#"
4 foo := `cat data`
5 
6 linewise bar=`cat data`: shebang
7   echo expression: {{foo}}
8   echo default: {{bar}}
9   echo linewise: `cat data`
10 
11 shebang:
12   #!/usr/bin/env sh
13   echo "shebang:" `cat data`
14 "#;
15 
16 const DATA: &str = "OK";
17 
18 const WANT: &str = "shebang: OK\nexpression: OK\ndefault: OK\nlinewise: OK\n";
19 
20 /// Test that just runs with the correct working directory when invoked with
21 /// `--justfile` but not `--working-directory`
22 #[test]
justfile_without_working_directory() -> Result<(), Box<dyn Error>>23 fn justfile_without_working_directory() -> Result<(), Box<dyn Error>> {
24   let tmp = temptree! {
25     justfile: JUSTFILE,
26     data: DATA,
27   };
28 
29   let output = Command::new(executable_path("just"))
30     .arg("--justfile")
31     .arg(&tmp.path().join("justfile"))
32     .output()?;
33 
34   if !output.status.success() {
35     eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
36     panic!();
37   }
38 
39   let stdout = String::from_utf8(output.stdout).unwrap();
40 
41   assert_eq!(stdout, WANT);
42 
43   Ok(())
44 }
45 
46 /// Test that just runs with the correct working directory when invoked with
47 /// `--justfile` but not `--working-directory`, and justfile path has no parent
48 #[test]
justfile_without_working_directory_relative() -> Result<(), Box<dyn Error>>49 fn justfile_without_working_directory_relative() -> Result<(), Box<dyn Error>> {
50   let tmp = temptree! {
51     justfile: JUSTFILE,
52     data: DATA,
53   };
54 
55   let output = Command::new(executable_path("just"))
56     .current_dir(&tmp.path())
57     .arg("--justfile")
58     .arg("justfile")
59     .output()?;
60 
61   if !output.status.success() {
62     eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
63     panic!();
64   }
65 
66   let stdout = String::from_utf8(output.stdout).unwrap();
67 
68   assert_eq!(stdout, WANT);
69 
70   Ok(())
71 }
72 
73 /// Test that just invokes commands from the directory in which the justfile is
74 /// found
75 #[test]
change_working_directory_to_search_justfile_parent() -> Result<(), Box<dyn Error>>76 fn change_working_directory_to_search_justfile_parent() -> Result<(), Box<dyn Error>> {
77   let tmp = temptree! {
78     justfile: JUSTFILE,
79     data: DATA,
80     subdir: {},
81   };
82 
83   let output = Command::new(executable_path("just"))
84     .current_dir(tmp.path().join("subdir"))
85     .output()?;
86 
87   if !output.status.success() {
88     eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
89     panic!();
90   }
91 
92   let stdout = String::from_utf8_lossy(&output.stdout);
93 
94   assert_eq!(stdout, WANT);
95 
96   Ok(())
97 }
98 
99 /// Test that just runs with the correct working directory when invoked with
100 /// `--justfile` but not `--working-directory`
101 #[test]
justfile_and_working_directory() -> Result<(), Box<dyn Error>>102 fn justfile_and_working_directory() -> Result<(), Box<dyn Error>> {
103   let tmp = temptree! {
104     justfile: JUSTFILE,
105     sub: {
106       data: DATA,
107     },
108   };
109 
110   let output = Command::new(executable_path("just"))
111     .arg("--justfile")
112     .arg(&tmp.path().join("justfile"))
113     .arg("--working-directory")
114     .arg(&tmp.path().join("sub"))
115     .output()?;
116 
117   if !output.status.success() {
118     eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
119     panic!();
120   }
121 
122   let stdout = String::from_utf8(output.stdout).unwrap();
123 
124   assert_eq!(stdout, WANT);
125 
126   Ok(())
127 }
128 
129 /// Test that just runs with the correct working directory when invoked with
130 /// `--justfile` but not `--working-directory`
131 #[test]
search_dir_child() -> Result<(), Box<dyn Error>>132 fn search_dir_child() -> Result<(), Box<dyn Error>> {
133   let tmp = temptree! {
134     child: {
135       justfile: JUSTFILE,
136       data: DATA,
137     },
138   };
139 
140   let output = Command::new(executable_path("just"))
141     .current_dir(&tmp.path())
142     .arg("child/")
143     .output()?;
144 
145   if !output.status.success() {
146     eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
147     panic!();
148   }
149 
150   let stdout = String::from_utf8(output.stdout).unwrap();
151 
152   assert_eq!(stdout, WANT);
153 
154   Ok(())
155 }
156 
157 /// Test that just runs with the correct working directory when invoked with
158 /// `--justfile` but not `--working-directory`
159 #[test]
search_dir_parent() -> Result<(), Box<dyn Error>>160 fn search_dir_parent() -> Result<(), Box<dyn Error>> {
161   let tmp = temptree! {
162     child: {
163     },
164     justfile: JUSTFILE,
165     data: DATA,
166   };
167 
168   let output = Command::new(executable_path("just"))
169     .current_dir(&tmp.path().join("child"))
170     .arg("../")
171     .output()?;
172 
173   if !output.status.success() {
174     eprintln!("{:?}", String::from_utf8_lossy(&output.stderr));
175     panic!();
176   }
177 
178   let stdout = String::from_utf8(output.stdout).unwrap();
179 
180   assert_eq!(stdout, WANT);
181 
182   Ok(())
183 }
184