-
Notifications
You must be signed in to change notification settings - Fork 6
add intellij integration test structure #76
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
Changes from 16 commits
8c3e2d7
bfa026c
0c28fcd
f67bb03
6e9c640
ca38644
2bf2a01
169b1ac
27f71ed
c7a5da9
e0fc2a7
dcfd8c6
ed7e2ac
f8c7106
d97d808
f6597fb
3064671
317502c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: Tests | ||
on: | ||
pull_request: | ||
|
||
permissions: | ||
contents: read | ||
checks: write | ||
pull-requests: write | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Java | ||
uses: actions/setup-java@v4 | ||
with: | ||
distribution: 'temurin' | ||
java-version: 17 | ||
cache: 'gradle' | ||
|
||
- name: Install Clojure | ||
uses: DeLaGuardo/setup-clojure@master | ||
with: | ||
bb: '1.12.196' | ||
cli: 1.12.0.1530 | ||
|
||
- name: Run tests | ||
run: ./gradlew test | ||
|
||
- name: Publish Test Report | ||
uses: mikepenz/action-junit-report@v5 | ||
if: success() || failure() # always run even if the previous step fails | ||
with: | ||
report_paths: '**/build/test-results/test/TEST-*.xml' | ||
simplified_summary: true | ||
comment: false |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{:paths ["src/scripts"] | ||
:tasks {tag scripts/tag | ||
build-plugin scripts/build-plugin | ||
test scripts/tests | ||
install-plugin scripts/install-plugin | ||
publish-plugin scripts/publish-plugin}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
(ns com.github.clojure-lsp.intellij.slurp-action-test | ||
(:require | ||
[clojure.test :refer [deftest is]] | ||
[com.github.clojure-lsp.intellij.editor :as editor] | ||
[com.github.clojure-lsp.intellij.test-utils :as test-utils] | ||
[com.github.ericdallo.clj4intellij.test :as clj4intellij.test]) | ||
(:import | ||
[com.intellij.openapi.project Project] | ||
[com.intellij.testFramework.fixtures CodeInsightTestFixture])) | ||
|
||
(set! *warn-on-reflection* true) | ||
|
||
(deftest slurp-action-test | ||
"Tests the Forward Slurp editor action functionality in Clojure LSP. | ||
This test: | ||
1. Sets up a test project with a Clojure file | ||
2. Opens the file in the editor | ||
3. Sets up the LSP server | ||
4. Moves the caret to a specific position | ||
5. Executes the Forward Slurp action | ||
6. Verifies the resulting text matches the expected output | ||
|
||
The test ensures that the Forward Slurp action correctly modifies the code structure | ||
by moving the closing parenthesis forward." | ||
(let [project-name "clojure.core" | ||
{:keys [fixture project deps-file]} (test-utils/setup-test-project project-name) | ||
clj-file (.copyFileToProject ^CodeInsightTestFixture fixture "foo.clj")] | ||
(is (= project-name (.getName ^Project project))) | ||
(is deps-file) | ||
|
||
(let [editor (test-utils/open-file-in-editor fixture clj-file)] | ||
(test-utils/setup-lsp-server project) | ||
(editor/move-caret-to-position editor 2 8) | ||
(test-utils/run-editor-action "ClojureLSP.ForwardSlurp" project) | ||
(clj4intellij.test/dispatch-all) | ||
(println (test-utils/get-editor-text fixture)) | ||
afucher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(.checkResultByFile ^CodeInsightTestFixture fixture "foo_expected.clj") | ||
(test-utils/teardown-test-project project)))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
(ns com.github.clojure-lsp.intellij.test-utils | ||
(:require | ||
[com.github.clojure-lsp.intellij.client :as lsp-client] | ||
[com.github.clojure-lsp.intellij.server :as server] | ||
[com.github.ericdallo.clj4intellij.app-manager :as app-manager] | ||
[com.github.ericdallo.clj4intellij.test :as clj4intellij.test]) | ||
(:import | ||
[com.github.clojure_lsp.intellij.extension SettingsState] | ||
[com.intellij.ide DataManager] | ||
[com.intellij.openapi.actionSystem ActionManager] | ||
[com.intellij.openapi.components ServiceManager] | ||
[com.intellij.testFramework.fixtures CodeInsightTestFixture])) | ||
|
||
(set! *warn-on-reflection* true) | ||
|
||
(defn get-editor-text | ||
"Returns the text content of the editor's document." | ||
[^CodeInsightTestFixture fixture] | ||
(-> fixture .getEditor .getDocument .getText)) | ||
|
||
(defn open-file-in-editor | ||
"Opens a file in the editor and returns the editor instance." | ||
[^CodeInsightTestFixture fixture file] | ||
(let [project (.getProject fixture)] | ||
(app-manager/write-command-action | ||
project | ||
(fn [] (.openFileInEditor fixture file))) | ||
(.getEditor fixture))) | ||
|
||
(defn run-editor-action | ||
"Runs an editor action with the given ID for the specified project." | ||
[action-id project] | ||
(let [action (.getAction (ActionManager/getInstance) action-id) | ||
context (.getDataContext (DataManager/getInstance))] | ||
(println "Running action:" action-id) | ||
(app-manager/write-command-action | ||
project | ||
(fn [] | ||
(.actionPerformed | ||
action | ||
(com.intellij.openapi.actionSystem.AnActionEvent/createFromDataContext action-id nil context)))))) | ||
|
||
(defn wait-lsp-start | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some reason using the same approach from clj4intellij with promises makes the test to hang forever 😢 So, we decided to create a specific function in this project to use a timeout instead of promise to wait. |
||
"Dispatches all events until the LSP server is started or the timeout is reached." | ||
[{:keys [project millis timeout] | ||
:or {millis 1000 | ||
timeout 10000}}] | ||
(let [start-time (System/currentTimeMillis)] | ||
(loop [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clojure.core.async/alts! is perfect for those do or timeout task, but it's ok to keep it as it is |
||
(let [current-time (System/currentTimeMillis) | ||
elapsed-time (- current-time start-time) | ||
status (lsp-client/server-status project)] | ||
(cond | ||
(>= elapsed-time timeout) | ||
(throw (ex-info "LSP server failed to start within timeout" | ||
{:elapsed-time elapsed-time | ||
:final-status status})) | ||
|
||
(= status :started) | ||
true | ||
|
||
:else | ||
(do | ||
(clj4intellij.test/dispatch-all) | ||
(Thread/sleep millis) | ||
(recur))))))) | ||
|
||
(defn teardown-test-project | ||
"Shuts down all resources for the given project." | ||
[project] | ||
(server/shutdown! project)) | ||
|
||
(defn setup-test-project | ||
"Sets up a test project with the given name and optional deps.edn content. | ||
Returns a map with :fixture, :project, and :deps-file." | ||
([project-name] | ||
(setup-test-project project-name "{}")) | ||
([project-name deps-content] | ||
(let [fixture (clj4intellij.test/setup project-name) | ||
deps-file (.createFile fixture "deps.edn" deps-content) | ||
_ (.setTestDataPath fixture "testdata") | ||
afucher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
project (.getProject fixture)] | ||
{:fixture fixture | ||
:project project | ||
:deps-file deps-file}))) | ||
|
||
(defn setup-lsp-server | ||
"Sets up and waits for the LSP server to be ready." | ||
[project] | ||
(let [my-settings (ServiceManager/getService SettingsState)] | ||
(.loadState my-settings my-settings) | ||
(clj4intellij.test/dispatch-all) | ||
(wait-lsp-start {:project project}))) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(ns foo) | ||
|
||
(println) "Oiii" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(ns foo) | ||
|
||
(println "Oiii") |
Uh oh!
There was an error while loading. Please reload this page.