diff --git a/.gitignore b/.gitignore index 96d907bc..335cecb7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /config.mk *autoloads.el .stack-work/ +test/test-project/target diff --git a/README.md b/README.md index c29ef978..f26df8fc 100644 --- a/README.md +++ b/README.md @@ -516,6 +516,7 @@ before being used. - `rustic-cargo-bench` run 'cargo bench' for the current project - `rustic-cargo-build-doc` build the documentation for the current project - `rustic-cargo-doc` open the documentation for the current project in a browser +- `rustic-cargo-lints` called with `rustic-lints-arguments` ## Clippy diff --git a/rustic-babel.el b/rustic-babel.el index 6cb9830e..52306f26 100644 --- a/rustic-babel.el +++ b/rustic-babel.el @@ -364,7 +364,7 @@ kill the running process." (let ((default-directory dir) (toolchain (cdr (assq :toolchain params)))) (write-region - (concat "#![allow(non_snake_case)]\n" + (concat "#![allow(non_snake_case, unused)]\n" (if use-blocks (rustic-babel-insert-mod use-blocks) "") (if include-blocks (rustic-babel-include-blocks include-blocks) "") (if wrap-main (rustic-babel-ensure-main-wrap body) body)) diff --git a/rustic-cargo.el b/rustic-cargo.el index b5e6ed86..d524eb0c 100644 --- a/rustic-cargo.el +++ b/rustic-cargo.el @@ -40,6 +40,11 @@ If nil then the project is simply created." :type 'boolean :group 'rustic-cargo) +(defcustom rustic-default-test-arguments "--workspace --benches --tests --all-features" + "Default arguments when running 'cargo test'." + :type 'string + :group 'rustic-cargo) + (defcustom rustic-cargo-check-arguments "--workspace --benches --tests --all-features" "Default arguments when running 'cargo check'." :type 'string @@ -90,6 +95,14 @@ stored in this variable.") (when rustic-cargo-test-disable-warnings (setq-local rustic-compile-rustflags "-Awarnings"))) +(defun rustic-cargo-run-test (test) + "Run TEST which can be a single test or mod name." + (let* ((c (list (rustic-cargo-bin) "test" test)) + (buf rustic-test-buffer-name) + (proc rustic-test-process-name) + (mode 'rustic-cargo-test-mode)) + (rustic-compilation c (list :buffer buf :process proc :mode mode)))) + ;;;###autoload (defun rustic-cargo-test-run (&optional test-args) "Start compilation process for 'cargo test' with optional TEST-ARGS." @@ -112,10 +125,13 @@ When calling this function from `rustic-popup-mode', always use the value of (interactive "P") (rustic-cargo-test-run (cond (arg - (setq rustic-test-arguments (read-from-minibuffer "Cargo test arguments: " rustic-test-arguments))) + (setq rustic-test-arguments (read-from-minibuffer "Cargo test arguments: " rustic-default-test-arguments))) ((eq major-mode 'rustic-popup-mode) - rustic-test-arguments) - (t "")))) + (if (> (length rustic-test-arguments) 0) + rustic-test-arguments + rustic-default-test-arguments)) + (t + rustic-default-test-arguments)))) ;;;###autoload (defun rustic-cargo-test-rerun () @@ -141,15 +157,6 @@ When calling this function from `rustic-popup-mode', always use the value of (rustic-cargo--get-current-mod))) (rustic-cargo-test))) -(defun rustic-cargo-run-test (test) - "Run TEST which can be a single test or mod name." - (let* ((command (list (rustic-cargo-bin) "test" test)) - (c (append command)) - (buf rustic-test-buffer-name) - (proc rustic-test-process-name) - (mode 'rustic-cargo-test-mode)) - (rustic-compilation c (list :buffer buf :process proc :mode mode)))) - (defconst rustic-cargo-mod-regexp "^\s*mod\s+\\([[:word:][:multibyte:]_][[:word:][:multibyte:]_[:digit:]]*\\)\s*{") (defconst rustic-cargo-fn-regexp @@ -235,7 +242,7 @@ Execute process in PATH." (inhibit-read-only t)) (make-process :name rustic-cargo-outdated-process-name :buffer buf - :command '("cargo" "outdated" "--depth" "1") + :command `(,(rustic-cargo-bin) "outdated" "--depth" "1") :filter #'rustic-cargo-outdated-filter :sentinel #'rustic-cargo-outdated-sentinel :file-handler t) @@ -287,7 +294,7 @@ Execute process in PATH." "Ask whether to install crate CRATE." (let ((cmd (format "cargo install cargo-%s" crate))) (when (yes-or-no-p (format "Cargo-%s missing. Install ? " crate)) - (async-shell-command cmd "cargo" "cargo-error")))) + (async-shell-command cmd (rustic-cargo-bin) "cargo-error")))) (defun rustic-cargo-outdated-generate-menu (packages) "Re-populate the `tabulated-list-entries' with PACKAGES." @@ -486,15 +493,15 @@ When calling this function from `rustic-popup-mode', always use the value of (defun rustic-cargo-run-get-relative-example-name () "Run 'cargo run --example' if current buffer within a 'examples' directory." - (if rustic--buffer-workspace - (let ((relative-filenames - (split-string (file-relative-name buffer-file-name rustic--buffer-workspace) "/"))) - (if (string= "examples" (car relative-filenames)) - (let ((size (length relative-filenames))) - (cond ((eq size 2) (file-name-sans-extension(nth 1 relative-filenames))) ;; examples/single-example1.rs - ((> size 2) (car (nthcdr (- size 2) relative-filenames))) ;; examples/example2/main.rs - (t nil))) nil)) - nil)) + (let* ((buffer-project-root (rustic-buffer-crate)) + (relative-filenames + (if buffer-project-root + (split-string (file-relative-name buffer-file-name buffer-project-root) "/") nil))) + (if (and relative-filenames (string= "examples" (car relative-filenames))) + (let ((size (length relative-filenames))) + (cond ((eq size 2) (file-name-sans-extension (nth 1 relative-filenames))) ;; examples/single-example1.rs + ((> size 2) (car (nthcdr (- size 2) relative-filenames))) ;; examples/example2/main.rs + (t nil))) nil))) ;;;###autoload (defun rustic-run-shell-command (&optional arg) diff --git a/rustic-clippy.el b/rustic-clippy.el index 6dcd067d..3d221e5a 100644 --- a/rustic-clippy.el +++ b/rustic-clippy.el @@ -17,6 +17,11 @@ :type 'string :group 'rustic-cargo) +(defcustom rustic-lints-arguments "-f custom_lints.toml clippy" + "Default arguments when running cargo-lints." + :type 'string + :group 'rustic-cargo) + (defvar rustic-clippy-process-name "rustic-cargo-clippy-process" "Process name for clippy processes.") @@ -53,6 +58,18 @@ :sentinel (plist-get args :sentinel) :no-display (plist-get args :silent))))) +;;;###autoload +(defun rustic-cargo-lints () + "Run cargo-lints with optional ARGS." + (interactive) + (let* ((command `(,(rustic-cargo-bin) + "lints" + ,@(split-string rustic-lints-arguments))) + (buf rustic-clippy-buffer-name) + (proc rustic-clippy-process-name) + (mode 'rustic-cargo-clippy-mode)) + (rustic-compilation command (list :buffer buf :process proc :mode mode)))) + ;;;###autoload (defun rustic-cargo-clippy (&optional arg) "Run 'cargo clippy'. @@ -63,7 +80,7 @@ When calling this function from `rustic-popup-mode', always use the value of (interactive "P") (rustic-cargo-clippy-run :params (cond (arg - (setq rustic-clippy-arguments (read-from-minibuffer "Cargo clippy arguments: " rustic-clippy-arguments))) + (setq rustic-clippy-arguments (read-from-minibuffer "Cargo clippy arguments: " rustic-default-clippy-arguments))) ((eq major-mode 'rustic-popup-mode) (if (> (length rustic-clippy-arguments) 0) rustic-clippy-arguments diff --git a/rustic-compile.el b/rustic-compile.el index e85ef1cb..e40587f2 100644 --- a/rustic-compile.el +++ b/rustic-compile.el @@ -237,6 +237,7 @@ Set environment variables for rust process." (set-process-sentinel process (plist-get args :sentinel)) (set-process-coding-system process 'utf-8-emacs-unix 'utf-8-emacs-unix) (process-put process 'command (plist-get args :command)) + (process-put process 'file-buffer (plist-get args :file-buffer)) process))) (defun rustic-compilation-setup-buffer (buf dir mode &optional no-mode-line) @@ -283,7 +284,8 @@ ARGS is a plist that affects how the process is run. (process (or (plist-get args :process) rustic-compilation-process-name)) (mode (or (plist-get args :mode) 'rustic-compilation-mode)) (directory (or (plist-get args :directory) (funcall rustic-compile-directory-method))) - (sentinel (or (plist-get args :sentinel) #'compilation-sentinel))) + (sentinel (or (plist-get args :sentinel) #'compilation-sentinel)) + (file-buffer (current-buffer))) (rustic-compilation-setup-buffer buf directory mode) (setq next-error-last-buffer buf) (unless (plist-get args :no-display) @@ -294,6 +296,7 @@ ARGS is a plist that affects how the process is run. (rustic-make-process :name process :buffer buf :command command + :file-buffer file-buffer :filter #'rustic-compilation-filter :sentinel sentinel :file-handler t)))) diff --git a/rustic.el b/rustic.el index 2d97db19..b9a2e081 100644 --- a/rustic.el +++ b/rustic.el @@ -1,6 +1,6 @@ ;;; rustic.el --- Rust development environment -*-lexical-binding: t-*- -;; Version: 2.5 +;; Version: 2.6 ;; Author: Mozilla ;; ;; Keywords: languages diff --git a/test/rustic-cargo-test.el b/test/rustic-cargo-test.el index 5976f47b..c4658c8d 100644 --- a/test/rustic-cargo-test.el +++ b/test/rustic-cargo-test.el @@ -201,5 +201,29 @@ fn test21() { (buffer (process-buffer proc))) (while (eq (process-status proc) 'run) (sit-for 0.01)) + + (should (string= (s-join " " (process-get proc 'command)) + (concat (rustic-cargo-bin) " check " + rustic-cargo-check-arguments))) + (with-current-buffer buffer (should (string-match "^warning:\s" (buffer-substring-no-properties (point-min) (point-max))))))))) + +(ert-deftest rustic-cargo-test-test () + (let* ((string "mod tests { +#[test] +fn test() { +} +}") + (buf (rustic-test-count-error-helper-new string)) + (default-directory (buffer-file-name buf))) + (with-current-buffer buf + (let* ((proc (rustic-cargo-test)) + (buffer (process-buffer proc))) + (while (eq (process-status proc) 'run) + (sit-for 0.01)) + (with-current-buffer buffer + (should (eq major-mode 'rustic-cargo-test-mode))) + (should (string= (s-join " " (process-get proc 'command)) + (concat (rustic-cargo-bin) " test " + rustic-default-test-arguments))))))) diff --git a/test/rustic-compile-test.el b/test/rustic-compile-test.el index de3f405d..46e6c409 100644 --- a/test/rustic-compile-test.el +++ b/test/rustic-compile-test.el @@ -153,6 +153,7 @@ (should (= compilation-num-errors-found 1)))))) (ert-deftest rustic-test-cargo-test () + ;; NOTE: this doesn't seem to be the case anymore ;; compilation-num-errors-found would be 8 with regular compilaton mode ;; due to parsing issues https://github.com/rust-lang/rust-mode/pull/254 (let ((rustic-compile-backtrace "full")) @@ -209,19 +210,32 @@ (with-current-buffer (get-buffer rustic-test-buffer-name) (should (= compilation-num-errors-found 10)))))) -;; ;; TODO: parsing doesn't work -;; (ert-deftest rustic-test-count-warnings () -;; (let* ((string "fn main() { -;; let v1 = vec![1, 2, 3]; -;; let v2 = vec![1, 2, 3]; -;; }") -;; (default-directory (rustic-test-count-error-helper string)) -;; (proc (rustic-compilation-start (split-string "cargo build")))) -;; (while (eq (process-status proc) 'run) -;; (sit-for 0.1)) -;; (with-current-buffer (get-buffer rustic-compilation-buffer-name) -;; (should (= compilation-num-warnings-found 1))))) - - - - +(ert-deftest rustic-test-count-warnings () + (let* ((string "fn main() { + let v1 = vec![1, 2, 3]; + let v2 = vec![1, 2, 3]; + let v3 = vec![1, 2, 3]; + let v4 = vec![1, 2, 3]; + let v5 = vec![1, 2, 3]; + let v6 = vec![1, 2, 3]; + let v7 = vec![1, 2, 3]; + let v8 = vec![1, 2, 3]; + let v9 = vec![1, 2, 3]; + let v10 = vec![1, 2, 3]; + let v11 = vec![1, 2, 3]; + let v12 = vec![1, 2, 3]; + let v13 = vec![1, 2, 3]; + let v14 = vec![1, 2, 3]; + let v15 = vec![1, 2, 3]; + let v16 = vec![1, 2, 3]; + let v17 = vec![1, 2, 3]; + let v18 = vec![1, 2, 3]; + let v19 = vec![1, 2, 3]; + let v20 = vec![1, 2, 3]; + }") + (default-directory (rustic-test-count-error-helper string)) + (proc (rustic-compilation-start (split-string "cargo build")))) + (while (eq (process-status proc) 'run) + (sit-for 0.1)) + (with-current-buffer (get-buffer rustic-compilation-buffer-name) + (should (= compilation-num-warnings-found 20))))) diff --git a/test/rustic-workspace-test.el b/test/rustic-workspace-test.el index df9c3da6..016c7b52 100644 --- a/test/rustic-workspace-test.el +++ b/test/rustic-workspace-test.el @@ -1,11 +1,13 @@ ;; -*- lexical-binding: t -*- ;; Before editing, eval (load-file "test-helper.el") -(ert-deftest rust-test-workspace-location () +(ert-deftest rust-test-workspace-crate-location () (should (equal (funcall rustic-compile-directory-method) default-directory)) - (let* ((test-workspace (expand-file-name "test/test-project/test-workspace/" default-directory)) - (default-directory test-workspace)) - (should (equal (funcall rustic-compile-directory-method) test-workspace)))) + (let* ((test-workspace (expand-file-name "test/test-project/")) + (test-crate (expand-file-name "test/test-project/crates/test-crate/"))) + (let ((default-directory (expand-file-name "src" test-crate))) + (should (string= (rustic-buffer-workspace) test-workspace)) + (should (string= (rustic-buffer-crate) test-crate))))) ;; just test if project-root function works for different versions (ert-deftest rust-test-project-root () diff --git a/test/test-project/Cargo.lock b/test/test-project/Cargo.lock new file mode 100644 index 00000000..be5aa8f9 --- /dev/null +++ b/test/test-project/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "test-workspace" +version = "0.0.1" diff --git a/test/test-project/Cargo.toml b/test/test-project/Cargo.toml index f741b14a..2abd9359 100644 --- a/test/test-project/Cargo.toml +++ b/test/test-project/Cargo.toml @@ -1 +1,2 @@ -# Dummy file needed for test +[workspace] +members = ["crates/*"] \ No newline at end of file diff --git a/test/test-project/crates/test-crate/Cargo.toml b/test/test-project/crates/test-crate/Cargo.toml new file mode 100644 index 00000000..3223ebea --- /dev/null +++ b/test/test-project/crates/test-crate/Cargo.toml @@ -0,0 +1,3 @@ +[package] +name = "test-workspace" +version = "0.0.1" \ No newline at end of file diff --git a/test/test-project/crates/test-crate/src/lib.rs b/test/test-project/crates/test-crate/src/lib.rs new file mode 100644 index 00000000..c7452f6f --- /dev/null +++ b/test/test-project/crates/test-crate/src/lib.rs @@ -0,0 +1 @@ +uuse libc; diff --git a/test/test-project/test-workspace/Cargo.toml b/test/test-project/test-workspace/Cargo.toml deleted file mode 100644 index f741b14a..00000000 --- a/test/test-project/test-workspace/Cargo.toml +++ /dev/null @@ -1 +0,0 @@ -# Dummy file needed for test