-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtests.rs
More file actions
160 lines (145 loc) · 5.35 KB
/
tests.rs
File metadata and controls
160 lines (145 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (C) 2025 Bryan A. Jones.
//
// This file is part of the CodeChat Editor. The CodeChat Editor is free
// software: you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// The CodeChat Editor is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// the CodeChat Editor. If not, see
// [http://www.gnu.org/licenses](http://www.gnu.org/licenses).
/// `test.rs` -- Unit tests for the vscode interface
/// ============================================================================
// Imports
// -----------------------------------------------------------------------------
//
// ### Standard library
use std::path::{MAIN_SEPARATOR_STR, PathBuf};
#[cfg(not(target_os = "macos"))]
use std::{thread::sleep, time::Duration};
// ### Third-party
#[cfg(not(target_os = "macos"))]
use assert_cmd::Command;
use assertables::{assert_ends_with, assert_not_contains, assert_starts_with};
// ### Local
use super::{path_to_url, url_to_path};
use crate::ide::{filewatcher::FILEWATCHER_PATH_PREFIX, vscode::tests::IP_PORT};
use test_utils::{cast, prep_test_dir};
// Support functions
// -----------------------------------------------------------------------------
//
// The lint on using `cargo_bin` doesn't apply, since this is only available for
// integration tests per the
// [docs](https://docs.rs/assert_cmd/latest/assert_cmd/cargo/macro.cargo_bin_cmd.html).
// Text of the warning:
//
// ```
// warning: use of deprecated associated function `assert_cmd::Command::cargo_bin`:
// incompatible with a custom cargo build-dir, see instead `cargo::cargo_bin_cmd!`
// ```
#[cfg(not(target_os = "macos"))]
#[allow(deprecated)]
fn get_server() -> Command {
Command::cargo_bin(assert_cmd::pkg_name!()).unwrap()
}
// Tests
// -----------------------------------------------------------------------------
#[test]
fn test_url_to_path() {
let (temp_dir, test_dir) = prep_test_dir!();
// Test a non-existent path.
assert_eq!(
cast!(
url_to_path(
&format!(
"http://127.0.0.1:{IP_PORT}/fw/fsc/dummy_connection_id/{}path%20spaces/foo.py",
if cfg!(windows) { "C:/" } else { "" }
),
FILEWATCHER_PATH_PREFIX
),
Ok
),
PathBuf::from(format!(
"{}path spaces{MAIN_SEPARATOR_STR}foo.py",
if cfg!(windows) { "C:\\" } else { "/" }
),)
);
// Test a path with a backslash in it.
assert_eq!(
cast!(
url_to_path(
&format!(
"http://127.0.0.1:{IP_PORT}/fw/fsc/dummy_connection_id/{}foo%5Cbar.py",
if cfg!(windows) { "C:/" } else { "" }
),
FILEWATCHER_PATH_PREFIX
),
Ok
),
PathBuf::from(format!(
"{}foo%5Cbar.py",
if cfg!(windows) { "C:\\" } else { "/" }
),)
);
// Test an actual path.
let test_dir_str = test_dir.to_str().unwrap();
assert_eq!(
url_to_path(
&format!(
"http://127.0.0.1:{IP_PORT}/fw/fsc/dummy_connection_id/{test_dir_str}/test%20spaces.py"
),
FILEWATCHER_PATH_PREFIX
)
.unwrap()
.canonicalize()
.unwrap(),
PathBuf::from(format!("{test_dir_str}{MAIN_SEPARATOR_STR}test spaces.py"))
.canonicalize()
.unwrap()
);
// Report any errors produced when removing the temporary directory.
temp_dir.close().unwrap();
}
#[test]
fn test_path_to_url() {
let (temp_dir, test_dir) = prep_test_dir!();
let mut file_path = test_dir.clone();
file_path.push("test spaces.py");
let url = path_to_url("/a/b", Some("conn1"), &file_path);
assert_starts_with!(url, "/a/b/conn1/");
assert_ends_with!(url, "test_path_to_url/test%20spaces.py");
// There shouldn't be a double forward slash in the name.
assert_not_contains!(url, "//");
// Report any errors produced when removing the temporary directory.
temp_dir.close().unwrap();
}
// Test startup outside the repo path. For some reason, this fails
// intermittently on Mac. Ignore these failures.
#[cfg(not(target_os = "macos"))]
#[test]
fn test_other_path() {
let (temp_dir, test_dir) = prep_test_dir!();
// Start the server. Calling `output()` causes the program to hang; call
// `status()` instead. Since the `assert_cmd` crates doesn't offer this, use
// the std lib instead.
std::process::Command::new(get_server().get_program())
.args(["--port", "8083", "start"])
.current_dir(&test_dir)
.status()
.expect("failed to start server");
// Stop it.
get_server()
.args(["--port", "8083", "stop"])
.current_dir(&test_dir)
.assert()
.success();
// Wait for the server to exit, since it locks the temp\_dir.
sleep(Duration::from_millis(3000));
// Report any errors produced when removing the temporary directory.
temp_dir.close().unwrap();
}