Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/merge-reports'
Browse files Browse the repository at this point in the history
  • Loading branch information
sviridov committed Jun 3, 2015
2 parents ed768e8 + 4ac2164 commit 63cb0f7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 28 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ A test coverage library for [Emacs Lisp](http://www.gnu.org/software/emacs/manua
$ COVERALLS_REPO_TOKEN=<your-coveralls-repo-token> cask exec ert-runner
```

- Set `report-file` option if you want to save coverage report locally:
- Set `report-file` option if you want to change report location:

```lisp
(undercover "*.el" (:report-file "/tmp/local-report.json"))
```

`undercover.el` will try to merge new report with existing one.

- Set `send-report` option to `nil` if you want to save coverage report locally:

```lisp
(undercover "*.el" (:send-report nil))
```
55 changes: 32 additions & 23 deletions test/first-example-library-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@

(with-env-variable "TRAVIS" "true"
(let ((undercover-force-coverage nil))
(undercover "test/first-example-library/*.el" (:report-file first-example-library-report-file))
(undercover "test/first-example-library/*.el"
(:report-file first-example-library-report-file)
(:send-report nil))
(ignore-errors (delete-file first-example-library-report-file))
(add-to-list 'load-path (file-truename "test/first-example-library"))
(require 'first-example-library)))

(defun assoc-cdr (key alist) (cdr (assoc key alist)))

(ert-deftest test-1/edebug-handlers-are-setted ()
(should (eq 'undercover--stop-point-before (symbol-function 'edebug-before)))
(should (eq 'undercover--stop-point-after (symbol-function 'edebug-after))))
Expand Down Expand Up @@ -78,31 +79,39 @@
(undercover-safe-report)
(ad-activate 'undercover-safe-report))

(let ((report (json-read-file first-example-library-report-file)))
(should (string-equal "travis-ci" (assoc-cdr 'service_name report)))
(let ((file-report (aref (assoc-cdr 'source_files report) 0)))
(should (string-equal "test/first-example-library/first-example-library.el"
(assoc-cdr 'name file-report)))
(let* ((json-object-type 'hash-table)
(json-array-type 'list)
(report (json-read-file first-example-library-report-file)))

(cl-flet ((check-lines-statistics (multiplier example-library-statistics)
;; distance statistics
(dolist (line '(14 15 16 17 18 19))
(should (= (* multiplier 2) (nth line example-library-statistics))))

(should-not (nth 13 example-library-statistics))
(should-not (nth 20 example-library-statistics))

;; fib statistics
(should (= (* multiplier 27) (nth 23 example-library-statistics)))
(should (= (* multiplier 27) (nth 24 example-library-statistics)))
(should (= (* multiplier 21) (nth 25 example-library-statistics)))
(should (= (* multiplier 12) (nth 26 example-library-statistics)))))

(should (string-equal (save-excursion
(find-file (file-truename "test/first-example-library/first-example-library.el"))
(buffer-substring-no-properties (point-min) (point-max)))
(should (string-equal "travis-ci" (gethash "service_name" report)))

(assoc-cdr 'source file-report)))
(let ((file-report (car (gethash "source_files" report))))
(should (string-equal "test/first-example-library/first-example-library.el"
(gethash "name" file-report)))

(let ((example-library-statistics (assoc-cdr 'coverage file-report)))
;; distance statistics
(dolist (line '(14 15 16 17 18 19))
(should (= 2 (aref example-library-statistics line))))
(should (string-equal (save-excursion
(find-file (file-truename "test/first-example-library/first-example-library.el"))
(buffer-substring-no-properties (point-min) (point-max)))

(should-not (aref example-library-statistics 13))
(should-not (aref example-library-statistics 20))
(gethash "source" file-report)))

;; fib statistics
(should (= 27 (aref example-library-statistics 23)))
(should (= 27 (aref example-library-statistics 24)))
(should (= 21 (aref example-library-statistics 25)))
(should (= 12 (aref example-library-statistics 26)))))))
(check-lines-statistics 1 (gethash "coverage" file-report))
(undercover--merge-coveralls-reports report)
(check-lines-statistics 2 (gethash "coverage" file-report))))))

(ert-deftest test-8/should-error ()
(with-env-variable "TRAVIS" nil
Expand Down
42 changes: 38 additions & 4 deletions undercover.el
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

(defun undercover--make-hash-table (&rest keys-and-values)
"Create new hash-table and fill it from KEYS-AND-VALUES."
(apply #'undercover--fill-hash-table (make-hash-table) keys-and-values))
(apply #'undercover--fill-hash-table (make-hash-table :test 'equal) keys-and-values))

(defun undercover--wildcards-to-files (wildcards)
"Return list of files matched by WILDCARDS.
Expand Down Expand Up @@ -304,10 +304,43 @@ Values of that hash are number of covers."
(undercover--fill-hash-table report
"source_files" (mapcar #'undercover--coveralls-file-report undercover--files)))

(defun undercover--merge-coveralls-report-file-lines-coverage (old-coverage new-coverage)
"Merge test coverage for lines from OLD-COVERAGE and NEW-COVERAGE."
(loop for (old-line-coverage . new-line-coverage)
in (-zip-fill 0 old-coverage new-coverage)
collect (cond
((null old-line-coverage) new-line-coverage)
((null new-line-coverage) old-line-coverage)
(t (+ new-line-coverage old-line-coverage)))))

(defun undercover--merge-coveralls-report-file-coverage (old-file-hash source-files-report)
"Merge test coverage from OLD-FILE-HASH into SOURCE-FILES-REPORT."
(let* ((file-name (gethash "name" old-file-hash))
(old-coverage (gethash "coverage" old-file-hash))
(new-file-hash (--first (string-equal file-name (gethash "name" it))
source-files-report)))
(if new-file-hash
(undercover--fill-hash-table new-file-hash
"coverage" (undercover--merge-coveralls-report-file-lines-coverage
old-coverage (gethash "coverage" new-file-hash)))
(rplacd (last source-files-report)
(cons old-file-hash nil)))))

(defun undercover--merge-coveralls-reports (report)
"Merge test coverage REPORT with existing from `undercover--report-file-path'."
(ignore-errors
(let* ((json-object-type 'hash-table)
(json-array-type 'list)
(old-report (json-read-file undercover--report-file-path))
(new-source-files-report (gethash "source_files" report)))
(dolist (old-file-hash (gethash "source_files" old-report))
(undercover--merge-coveralls-report-file-coverage
old-file-hash new-source-files-report)))))

(defun undercover--create-coveralls-report ()
"Create test coverage report for coveralls.io."
(undercover--collect-files-coverage undercover--files)
(let ((report (make-hash-table)))
(let ((report (make-hash-table :test 'equal)))
(cond
((undercover--coveralls-repo-token)
(undercover--update-coveralls-report-with-repo-token report)
Expand All @@ -316,6 +349,7 @@ Values of that hash are number of covers."
(t (error "Unsupported coveralls.io report")))
(undercover--update-coveralls-report-with-git report)
(undercover--fill-coveralls-report report)
(undercover--merge-coveralls-reports report)
(json-encode report)))

(defun undercover--save-coveralls-report (json-report)
Expand Down Expand Up @@ -375,8 +409,8 @@ Return wildcards."
(--separate (or (stringp it) (eq :exclude (car-safe it))) configuration)
(dolist (option options wildcards)
(case (car-safe option)
(:report-file (setq undercover--send-report nil
undercover--report-file-path (cadr option)))
(:report-file (setq undercover--report-file-path (cadr option)))
(:send-report (setq undercover--send-report (cadr option)))
(otherwise (error "Unsupported option: %s" option))))))

(defun undercover--setup (configuration)
Expand Down

0 comments on commit 63cb0f7

Please sign in to comment.