Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Jump back and forth between Clojure source and its test #1168

Closed
rudolfvesely opened this issue May 5, 2021 · 9 comments
Closed
Labels
ux User experience related

Comments

@rudolfvesely
Copy link

Dear Calva developers,

As discussed on Clojureverse, I'd like to kindly ask to add the following feature:

  • A command called "Clojure source/test jump" with or without specified key binding.
  • When executed for example from a file src/a/b/c.clj Calva will open a file test/a/b/c_test.clj and then when executed again Calva will focus to the the source file src/a/b/c.clj.
  • If the test file is missing, Calva will ask to create it with the following content:
(ns a.b.c-test
  (:require [clojure.test :refer :all]
            [a.b.c :as t]))
  • If the source file is missing, Calva will ask to create it with the following content:
(ns a.b.c)
  • Files are created with all parent directories in case they're missing

There is already a similar extension for Elixir called Elixir Test that has exactly this functionality so you may consider to take a look at its code (MIT licensed) to see how it's implemented.

Thank you.

Kind regards,

Rudolf Vesely

@PEZ
Copy link
Collaborator

PEZ commented May 5, 2021

Thanks! There is some prior art in Calva as well, in the way the ctrl+alt+c o shortcut jumps between the output window and the source file of the current namespace.

@bpringe bpringe added the ux User experience related label May 6, 2021
@rudolfvesely
Copy link
Author

Output window jump is great. Thank you PEZ!

For those looking for the command name:

{
    "key": "ctrl+alt+c o",
    "command": "calva.showOutputWindow",
    "when": "calva:connected && calva:connected && calva:keybindingsEnabled && !calva:outputWindowActive"
},
{
    "key": "ctrl+alt+c o",
    "command": "calva.showFileForOutputWindowNS",
    "when": "calva:connected && calva:connected && calva:keybindingsEnabled && calva:outputWindowActive"
},

@pratik97
Copy link
Contributor

@PEZ @bpringe I'd like to work on this feature if it's not yet assigned. I looked at this module of Elixir test repo that @rudolfvesely mentioned and I can implement similar thing for Calva unless you guys have some other ideas.

@PEZ
Copy link
Collaborator

PEZ commented May 25, 2021

@pratik97 please do! And please holler or way in the calva channel over at the Clojurians slack.

pratik97 pushed a commit to pratik97/calva that referenced this issue May 26, 2021
@bpringe
Copy link
Member

bpringe commented May 26, 2021

Just moving this info from Slack: if we make the way this feature calculates/finds test namespaces account for more possibilities than just the test namespace being the same plus -test, maybe we should also consider changing the implementation of the command for running namespace tests, which I think only checks for the -test variety of test namespace:

if (!ns.endsWith('-test')) {

@seancorfield mentioned that there are different conventions used for test namespace and file organization:

and most test runners have options so you can tell them the directory structure and namespace pattern to use, so I think this really should expose those settings from the get-go

We should probably either add settings like this, or implement this in such a way that it accounts for a majority of cases.

@bpringe
Copy link
Member

bpringe commented May 26, 2021

Maybe this goes without saying, but I think we should keep this implementation as simple as possible so as to reduce future maintenance work/requests on it. If that means just implementing it for one convention, without settings, and documenting that it's just a convenience feature if you use x convention, I think that would be fine.

Also, thank you @pratik97 for working on this!

@PEZ
Copy link
Collaborator

PEZ commented May 26, 2021

I agree about starting simple, with few settings.

@seancorfield
Copy link
Contributor

The cases to consider:

  • Where are tests?
    • Many simple projects have just a src and a test folder so you can fairly safely go from /path/to/src/foo/bar.clj to /path/to/test/foo/bar_test.clj and back again.
    • However, a number of projects -- such as Contrib libraries and almost any projects that have Clojure + Java -- tend to have src/main and src/test -- or src/main/<lang> and src/test/<lang> -- so I would suggest that if you're in src/test/foo/bar_test.clj you'd want to look for src/main/foo/bar.clj and vice versa (test cases to bear in mind: src/main/foo/bar.cljs and test/main/foo/bar_test.cljs; src/main/cljs/foo/bar.cljs and test/main/cljs/foo/bar_test.cljs; both cases have foo.bar and foo.bar-test for their namespaces).
    • There are other patterns -- typically in repos that have .clj, .cljs, and .cljc -- but those mostly seem to be src/cljs/foo/bar.cljs and test/cljs/foo/bar_test.cljs so a swapping of src for test and addition of _test or removal of it are going to catch most cases. Caveat: again, with repos that have src/<lang>/foo/bar.clj*, the <lang> portion is not part of the namespace, but you should be able to follow the lead of whatever file you are in and not worry about the actual path (beyond making source and test match).
  • File extensions:
    • I think you'll get most cases by just keeping the same file extension between source and test files, and if you offer to create a new file, offer the same file extension as the "original", whichever direction you're going in.

I think you could do all of that without adding any options -- even though you'd need to support the "simple" style and "Maven" style paths (src/main & src/test).

This would be a reasonable test case: https://github.com/clojure/tools.cli -- it follows the Maven structure and uses .cljc files.

Not sure if you'd want to support this as a test case: https://github.com/clojure/data.xml -- the test files have test_ at the beginning of the file name (and test- in the namespace).

Of course, I would love to see this support additional test filename patterns (such as _expectations.clj* and -expectations in the namespace) but that's definitely a minority of users -- so for the first version of it, just supporting _test/-test should be fine.

LMK if you have any Qs about that.

@pratik97
Copy link
Contributor

makes sense, @seancorfield thanks for your inputs! I will work on adding extra look-up path and the file extensions.

pratik97 pushed a commit to pratik97/calva that referenced this issue May 27, 2021
pratik97 pushed a commit to pratik97/calva that referenced this issue Jun 4, 2021
pratik97 pushed a commit to pratik97/calva that referenced this issue Jun 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ux User experience related
Projects
None yet
Development

No branches or pull requests

5 participants