History

Rework prettier tests (#3160)

Do not infuse `FakeNodeRuntime` with prettier exceptions, rather keep the default formatter installation method as no-op.

Kirill Bulatov committed 3 years ago

Commit b090cef2 parents ff49781 e9ce9355 files · +16 −121Browse files

Unified · Split

5 files
  1. crates/collab/src/tests/integration_tests.rs +1 −5
  2. crates/editor/src/editor_tests.rs +2 −4
  3. crates/node_runtime/src/node_runtime.rs +7 −98
  4. crates/prettier/src/prettier.rs +4 −4
  5. crates/project/src/project.rs +2 −10
crates/collab/src/tests/integration_tests.rs+1−5
@@ -4555,11 +4555,7 @@ async fn test_prettier_formatting_buffer( · 4554 unmodified lines
4555 .insert_tree(&directory, json!({ "a.rs": buffer_text }))4555 .insert_tree(&directory, json!({ "a.rs": buffer_text }))
4556 .await;4556 .await;
4557 let (project_a, worktree_id) = client_a.build_local_project(&directory, cx_a).await;4557 let (project_a, worktree_id) = client_a.build_local_project(&directory, cx_a).await;
4558 let prettier_format_suffix = project_a.update(cx_a, |project, _| {4558 let prettier_format_suffix = project::TEST_PRETTIER_FORMAT_SUFFIX;
4559 let suffix = project.enable_test_prettier(&[test_plugin]);
4560 project.languages().add(language);
4561 suffix
4562 });
4563 let buffer_a = cx_a4559 let buffer_a = cx_a
4564 .background()4560 .background()
4565 .spawn(project_a.update(cx_a, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx)))4561 .spawn(project_a.update(cx_a, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx)))
crates/editor/src/editor_tests.rs+2−4
@@ -5117,7 +5117,6 @@ async fn test_document_format_manual_trigger(cx: &mut gpui::TestAppContext) { · 5116 unmodified lines
5117 5117
5118 let project = Project::test(fs, ["/file.rs".as_ref()], cx).await;5118 let project = Project::test(fs, ["/file.rs".as_ref()], cx).await;
5119 project.update(cx, |project, _| {5119 project.update(cx, |project, _| {
5120 project.enable_test_prettier(&[]);
5121 project.languages().add(Arc::new(language));5120 project.languages().add(Arc::new(language));
5122 });5121 });
5123 let buffer = project5122 let buffer = project
@@ -7864,10 +7863,9 @@ async fn test_document_format_with_prettier(cx: &mut gpui::TestAppContext) { · 2740 unmodified lines
7864 fs.insert_file("/file.rs", Default::default()).await;7863 fs.insert_file("/file.rs", Default::default()).await;
7865 7864
7866 let project = Project::test(fs, ["/file.rs".as_ref()], cx).await;7865 let project = Project::test(fs, ["/file.rs".as_ref()], cx).await;
7867 let prettier_format_suffix = project.update(cx, |project, _| {7866 let prettier_format_suffix = project::TEST_PRETTIER_FORMAT_SUFFIX;
7868 let suffix = project.enable_test_prettier(&[test_plugin]);7867 project.update(cx, |project, _| {
7869 project.languages().add(Arc::new(language));7868 project.languages().add(Arc::new(language));
7870 suffix
7871 });7869 });
7872 let buffer = project7870 let buffer = project
7873 .update(cx, |project, cx| project.open_local_buffer("/file.rs", cx))7871 .update(cx, |project, cx| project.open_local_buffer("/file.rs", cx))
crates/node_runtime/src/node_runtime.rs+7−98
@@ -220,96 +220,31 @@ impl NodeRuntime for RealNodeRuntime { · 219 unmodified lines
220 }220 }
221}221}
222 222
223pub struct FakeNodeRuntime(Option<PrettierSupport>);223pub struct FakeNodeRuntime;
224
225struct PrettierSupport {
226 plugins: Vec<&'static str>,
227}
228 224
229impl FakeNodeRuntime {225impl FakeNodeRuntime {
230 pub fn new() -> Arc<dyn NodeRuntime> {226 pub fn new() -> Arc<dyn NodeRuntime> {
231 Arc::new(FakeNodeRuntime(None))227 Arc::new(Self)
232 }
233
234 pub fn with_prettier_support(plugins: &[&'static str]) -> Arc<dyn NodeRuntime> {
235 Arc::new(FakeNodeRuntime(Some(PrettierSupport::new(plugins))))
236 }228 }
237}229}
238 230
239#[async_trait::async_trait]231#[async_trait::async_trait]
240impl NodeRuntime for FakeNodeRuntime {232impl NodeRuntime for FakeNodeRuntime {
241 async fn binary_path(&self) -> anyhow::Result<PathBuf> {233 async fn binary_path(&self) -> anyhow::Result<PathBuf> {
242 if let Some(prettier_support) = &self.0 {234 unreachable!()
243 prettier_support.binary_path().await
244 } else {
245 unreachable!()
246 }
247 }235 }
248 236
249 async fn run_npm_subcommand(237 async fn run_npm_subcommand(
250 &self,238 &self,
251 directory: Option<&Path>,239 _: Option<&Path>,
252 subcommand: &str,240 subcommand: &str,
253 args: &[&str],241 args: &[&str],
254 ) -> anyhow::Result<Output> {242 ) -> anyhow::Result<Output> {
255 if let Some(prettier_support) = &self.0 {243 unreachable!("Should not run npm subcommand '{subcommand}' with args {args:?}")
256 prettier_support
257 .run_npm_subcommand(directory, subcommand, args)
258 .await
259 } else {
260 unreachable!()
261 }
262 }
263
264 async fn npm_package_latest_version(&self, name: &str) -> anyhow::Result<String> {
265 if let Some(prettier_support) = &self.0 {
266 prettier_support.npm_package_latest_version(name).await
267 } else {
268 unreachable!()
269 }
270 }
271
272 async fn npm_install_packages(
273 &self,
274 directory: &Path,
275 packages: &[(&str, &str)],
276 ) -> anyhow::Result<()> {
277 if let Some(prettier_support) = &self.0 {
278 prettier_support
279 .npm_install_packages(directory, packages)
280 .await
281 } else {
282 unreachable!()
283 }
284 }
285}
286
287impl PrettierSupport {
288 const PACKAGE_VERSION: &str = "0.0.1";
289
290 fn new(plugins: &[&'static str]) -> Self {
291 Self {
292 plugins: plugins.to_vec(),
293 }
294 }
295}
296
297#[async_trait::async_trait]
298impl NodeRuntime for PrettierSupport {
299 async fn binary_path(&self) -> anyhow::Result<PathBuf> {
300 Ok(PathBuf::from("prettier_fake_node"))
301 }
302
303 async fn run_npm_subcommand(&self, _: Option<&Path>, _: &str, _: &[&str]) -> Result<Output> {
304 unreachable!()
305 }244 }
306 245
307 async fn npm_package_latest_version(&self, name: &str) -> anyhow::Result<String> {246 async fn npm_package_latest_version(&self, name: &str) -> anyhow::Result<String> {
308 if name == "prettier" || self.plugins.contains(&name) {247 unreachable!("Should not query npm package '{name}' for latest version")
309 Ok(Self::PACKAGE_VERSION.to_string())
310 } else {
311 panic!("Unexpected package name: {name}")
312 }
313 }248 }
314 249
315 async fn npm_install_packages(250 async fn npm_install_packages(
@@ -317,32 +252,6 @@ impl NodeRuntime for PrettierSupport { · 1 unmodified line
317 _: &Path,252 _: &Path,
318 packages: &[(&str, &str)],253 packages: &[(&str, &str)],
319 ) -> anyhow::Result<()> {254 ) -> anyhow::Result<()> {
320 assert_eq!(255 unreachable!("Should not install packages {packages:?}")
321 packages.len(),
322 self.plugins.len() + 1,
323 "Unexpected packages length to install: {:?}, expected `prettier` + {:?}",
324 packages,
325 self.plugins
326 );
327 for (name, version) in packages {
328 assert!(
329 name == &"prettier" || self.plugins.contains(name),
330 "Unexpected package `{}` to install in packages {:?}, expected {} for `prettier` + {:?}",
331 name,
332 packages,
333 Self::PACKAGE_VERSION,
334 self.plugins
335 );
336 assert_eq!(
337 version,
338 &Self::PACKAGE_VERSION,
339 "Unexpected package version `{}` to install in packages {:?}, expected {} for `prettier` + {:?}",
340 version,
341 packages,
342 Self::PACKAGE_VERSION,
343 self.plugins
344 );
345 }
346 Ok(())
347 }256 }
348}257}
crates/prettier/src/prettier.rs+4−4
@@ -44,6 +44,9 @@ pub const PRETTIER_SERVER_JS: &str = include_str!("./prettier_server.js"); · 43 unmodified lines
44const PRETTIER_PACKAGE_NAME: &str = "prettier";44const PRETTIER_PACKAGE_NAME: &str = "prettier";
45const TAILWIND_PRETTIER_PLUGIN_PACKAGE_NAME: &str = "prettier-plugin-tailwindcss";45const TAILWIND_PRETTIER_PLUGIN_PACKAGE_NAME: &str = "prettier-plugin-tailwindcss";
46 46
47#[cfg(any(test, feature = "test-support"))]
48pub const FORMAT_SUFFIX: &str = "\nformatted by test prettier";
49
47impl Prettier {50impl Prettier {
48 pub const CONFIG_FILE_NAMES: &'static [&'static str] = &[51 pub const CONFIG_FILE_NAMES: &'static [&'static str] = &[
49 ".prettierrc",52 ".prettierrc",
@@ -60,9 +63,6 @@ impl Prettier { · 10 unmodified lines
60 ".editorconfig",63 ".editorconfig",
61 ];64 ];
62 65
63 #[cfg(any(test, feature = "test-support"))]
64 pub const FORMAT_SUFFIX: &str = "\nformatted by test prettier";
65
66 pub async fn locate(66 pub async fn locate(
67 starting_path: Option<LocateStart>,67 starting_path: Option<LocateStart>,
68 fs: Arc<dyn Fs>,68 fs: Arc<dyn Fs>,
@@ -349,7 +349,7 @@ impl Prettier { · 280 unmodified lines
349 #[cfg(any(test, feature = "test-support"))]349 #[cfg(any(test, feature = "test-support"))]
350 Self::Test(_) => Ok(buffer350 Self::Test(_) => Ok(buffer
351 .read_with(cx, |buffer, cx| {351 .read_with(cx, |buffer, cx| {
352 let formatted_text = buffer.text() + Self::FORMAT_SUFFIX;352 let formatted_text = buffer.text() + FORMAT_SUFFIX;
353 buffer.diff(formatted_text, cx)353 buffer.diff(formatted_text, cx)
354 })354 })
355 .await),355 .await),
crates/project/src/project.rs+2−10
@@ -86,6 +86,8 @@ use util::{ · 85 unmodified lines
86};86};
87 87
88pub use fs::*;88pub use fs::*;
89#[cfg(any(test, feature = "test-support"))]
90pub use prettier::FORMAT_SUFFIX as TEST_PRETTIER_FORMAT_SUFFIX;
89pub use worktree::*;91pub use worktree::*;
90 92
91pub trait Item {93pub trait Item {
@@ -833,16 +835,6 @@ impl Project { · 741 unmodified lines
833 project835 project
834 }836 }
835 837
836 /// Enables a prettier mock that avoids interacting with node runtime, prettier LSP wrapper, or any real file changes.
837 /// Instead, if appends the suffix to every input, this suffix is returned by this method.
838 #[cfg(any(test, feature = "test-support"))]
839 pub fn enable_test_prettier(&mut self, plugins: &[&'static str]) -> &'static str {
840 self.node = Some(node_runtime::FakeNodeRuntime::with_prettier_support(
841 plugins,
842 ));
843 Prettier::FORMAT_SUFFIX
844 }
845
846 fn on_settings_changed(&mut self, cx: &mut ModelContext<Self>) {838 fn on_settings_changed(&mut self, cx: &mut ModelContext<Self>) {
847 let mut language_servers_to_start = Vec::new();839 let mut language_servers_to_start = Vec::new();
848 let mut language_formatters_to_check = Vec::new();840 let mut language_formatters_to_check = Vec::new();

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t