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
45554555 .insert_tree(&directory, json!({ "a.rs": buffer_text }))
45564556 .await;
45574557 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, _| {
4559− let suffix = project.enable_test_prettier(&[test_plugin]);
4560− project.languages().add(language);
4561− suffix
4562− });
4558+ let prettier_format_suffix = project::TEST_PRETTIER_FORMAT_SUFFIX;
45634559 let buffer_a = cx_a
45644560 .background()
45654561 .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
51175117
51185118 let project = Project::test(fs, ["/file.rs".as_ref()], cx).await;
51195119 project.update(cx, |project, _| {
5120− project.enable_test_prettier(&[]);
51215120 project.languages().add(Arc::new(language));
51225121 });
51235122 let buffer = project
@@ -7864,10 +7863,9 @@ async fn test_document_format_with_prettier(cx: &mut gpui::TestAppContext) { · 2740 unmodified lines
78647863 fs.insert_file("/file.rs", Default::default()).await;
78657864
78667865 let project = Project::test(fs, ["/file.rs".as_ref()], cx).await;
7867− let prettier_format_suffix = project.update(cx, |project, _| {
7868− let suffix = project.enable_test_prettier(&[test_plugin]);
7866+ let prettier_format_suffix = project::TEST_PRETTIER_FORMAT_SUFFIX;
7867+ project.update(cx, |project, _| {
78697868 project.languages().add(Arc::new(language));
7870− suffix
78717869 });
78727870 let buffer = project
78737871 .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
220220 }
221221 }
222222
223−pub struct FakeNodeRuntime(Option<PrettierSupport>);
224−
225−struct PrettierSupport {
226− plugins: Vec<&'static str>,
227−}
223+pub struct FakeNodeRuntime;
228224
229225 impl FakeNodeRuntime {
230226 pub fn new() -> Arc<dyn NodeRuntime> {
231− Arc::new(FakeNodeRuntime(None))
232− }
233−
234− pub fn with_prettier_support(plugins: &[&'static str]) -> Arc<dyn NodeRuntime> {
235− Arc::new(FakeNodeRuntime(Some(PrettierSupport::new(plugins))))
227+ Arc::new(Self)
236228 }
237229 }
238230
239231 #[async_trait::async_trait]
240232 impl NodeRuntime for FakeNodeRuntime {
241233 async fn binary_path(&self) -> anyhow::Result<PathBuf> {
242− if let Some(prettier_support) = &self.0 {
243− prettier_support.binary_path().await
244− } else {
245− unreachable!()
246− }
234+ unreachable!()
247235 }
248236
249237 async fn run_npm_subcommand(
250238 &self,
251− directory: Option<&Path>,
239+ _: Option<&Path>,
252240 subcommand: &str,
253241 args: &[&str],
254242 ) -> anyhow::Result<Output> {
255− if let Some(prettier_support) = &self.0 {
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−
287−impl 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]
298−impl 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!()
243+ unreachable!("Should not run npm subcommand '{subcommand}' with args {args:?}")
305244 }
306245
307246 async fn npm_package_latest_version(&self, name: &str) -> anyhow::Result<String> {
308− if name == "prettier" || self.plugins.contains(&name) {
309− Ok(Self::PACKAGE_VERSION.to_string())
310− } else {
311− panic!("Unexpected package name: {name}")
312− }
247+ unreachable!("Should not query npm package '{name}' for latest version")
313248 }
314249
315250 async fn npm_install_packages(
@@ -317,32 +252,6 @@ impl NodeRuntime for PrettierSupport { · 1 unmodified line
317252 _: &Path,
318253 packages: &[(&str, &str)],
319254 ) -> anyhow::Result<()> {
320− assert_eq!(
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(())
255+ unreachable!("Should not install packages {packages:?}")
347256 }
348257 }
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
4444 const PRETTIER_PACKAGE_NAME: &str = "prettier";
4545 const TAILWIND_PRETTIER_PLUGIN_PACKAGE_NAME: &str = "prettier-plugin-tailwindcss";
4646
47+#[cfg(any(test, feature = "test-support"))]
48+pub const FORMAT_SUFFIX: &str = "\nformatted by test prettier";
49+
4750 impl Prettier {
4851 pub const CONFIG_FILE_NAMES: &'static [&'static str] = &[
4952 ".prettierrc",
@@ -60,9 +63,6 @@ impl Prettier { · 10 unmodified lines
6063 ".editorconfig",
6164 ];
6265
63− #[cfg(any(test, feature = "test-support"))]
64− pub const FORMAT_SUFFIX: &str = "\nformatted by test prettier";
65−
6666 pub async fn locate(
6767 starting_path: Option<LocateStart>,
6868 fs: Arc<dyn Fs>,
@@ -349,7 +349,7 @@ impl Prettier { · 280 unmodified lines
349349 #[cfg(any(test, feature = "test-support"))]
350350 Self::Test(_) => Ok(buffer
351351 .read_with(cx, |buffer, cx| {
352− let formatted_text = buffer.text() + Self::FORMAT_SUFFIX;
352+ let formatted_text = buffer.text() + FORMAT_SUFFIX;
353353 buffer.diff(formatted_text, cx)
354354 })
355355 .await),
crates/project/src/project.rs+2−10
@@ -86,6 +86,8 @@ use util::{ · 85 unmodified lines
8686 };
8787
8888 pub use fs::*;
89+#[cfg(any(test, feature = "test-support"))]
90+pub use prettier::FORMAT_SUFFIX as TEST_PRETTIER_FORMAT_SUFFIX;
8991 pub use worktree::*;
9092
9193 pub trait Item {
@@ -833,16 +835,6 @@ impl Project { · 741 unmodified lines
833835 project
834836 }
835837
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−
846838 fn on_settings_changed(&mut self, cx: &mut ModelContext<Self>) {
847839 let mut language_servers_to_start = Vec::new();
848840 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