Skip to content

Commit 0deb1ed

Browse files
authored
Merge pull request #74 from adamtornhill/57-specify-pre-condition-for-messages-analysis
Enforce the pre-conditions on the messages analysis
2 parents f415477 + a6b6be5 commit 0deb1ed

4 files changed

Lines changed: 58 additions & 11 deletions

File tree

src/code_maat/analysis/commit_messages.clj

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
(IllegalArgumentException.
3333
"Commit messages: you need to provide an expression to match against."))))
3434

35-
(defn- columns-of-interest
36-
"Filter away all the data we won't need (less GC overhead)."
37-
[ds]
38-
(dataset/-select-by [:entity :message] ds))
39-
4035
(defn- commit-matches
4136
"Performs a match for the expression provided by the
4237
user against a single commit message."
@@ -55,6 +50,22 @@
5550
frequencies
5651
(into [])))
5752

53+
(defn- commit-log-without-messages?
54+
[ds]
55+
(let [ncommits-with-message (->> ds (dataset/-where {:message {:$fn (fn [m] (not= m "-"))}}) dataset/-nrows)
56+
nrows (dataset/-nrows ds)]
57+
(and (pos? nrows)
58+
(= 0 ncommits-with-message))))
59+
60+
(defn- ensure-supported-vcs
61+
"The git2 format doesn't support this analysis since the commit messages aren't included. Here we make a more
62+
general check."
63+
[ds]
64+
(if (commit-log-without-messages? ds)
65+
(throw (IllegalArgumentException. (str "Wrong version-control format. Cannot do a messages analysis without commit messages. "
66+
"Look at the difference between the git and git2 formats in the docs.")))
67+
ds))
68+
5869
(defn by-word-frequency
5970
"Returns the frequencies of the given word matches
6071
across all entities.
@@ -65,7 +76,9 @@
6576
this function counts the occourences."
6677
[ds options]
6778
(->>
68-
(rows-matching-given-expr (match-expr-from options) ds)
69-
as-matching-entity-freqs
70-
(incanter/dataset [:entity :matches])
71-
(dataset/-order-by [:matches :entity] :desc)))
79+
ds
80+
ensure-supported-vcs
81+
(rows-matching-given-expr (match-expr-from options))
82+
as-matching-entity-freqs
83+
(incanter/dataset [:entity :matches])
84+
(dataset/-order-by [:matches :entity] :desc)))

src/code_maat/app/app.clj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@
210210
(output-fn! (analysis-fn changes))
211211
(catch AssertionError e ; typically a pre- or post-condition
212212
(throw-internal-error e))
213+
(catch IllegalArgumentException e
214+
(throw e))
213215
(catch Exception e
214216
(throw-internal-error e))))
215217

test/code_maat/analysis/commit_messages_test.clj

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
(ns code-maat.analysis.commit-messages-test
77
(:require [code-maat.analysis.commit-messages :as c]
88
[code-maat.analysis.test-data :as td]
9-
[code-maat.dataset.dataset :as ds])
9+
[code-maat.dataset.dataset :as ds]
10+
[incanter.core :as incanter])
1011
(:use clojure.test))
1112

1213
(defn- as-option
@@ -23,4 +24,26 @@
2324
(is (= (c/by-word-frequency td/vcsd (as-option "no match for this"))
2425
(ds/-dataset [:entity :matches]
2526
[]))))
27+
28+
(defn- run-messages-analysis
29+
[commits]
30+
(-> commits
31+
incanter/to-dataset
32+
(c/by-word-frequency (as-option "change"))))
33+
34+
(deftest detects-absent-message-fields ; not all supported version-control formats include a commit message
35+
(testing "No commit messages triggers exception"
36+
(is (thrown? IllegalArgumentException
37+
(run-messages-analysis [{:author "apt" :entity "A" :rev 1 :message "-"}
38+
{:author "apt" :entity "B" :rev 2 :message "-"}]))))
39+
(testing "Empy dataset is valid"
40+
(is (= (run-messages-analysis [])
41+
(ds/-dataset [:entity :matches]
42+
[]))))
43+
(testing "Valid, if any commit has a message"
44+
(is (= (run-messages-analysis [{:author "apt" :entity "A" :rev 1 :message "-"}
45+
{:author "apt" :entity "B" :rev 2 :message "some change message"}])
46+
(ds/-dataset [:entity :matches]
47+
[["B" 1]])))))
48+
2649

test/code_maat/end_to_end/scenario_tests.clj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@
106106
(is (= (run-with-str-output log-file options)
107107
"author,peer,shared,average,strength\nXYZ,APT,1,2,50\nAPT,XYZ,1,2,50\n")))
108108

109+
(defn- with-message-to-match
110+
[options msg]
111+
(assoc options :expression-to-match msg))
112+
113+
(deftest counts-commit-message-patterns
114+
(is (= (run-with-str-output git-log-file
115+
(with-message-to-match (git-options "messages")"stat"))
116+
"entity,matches\n/Infrastrucure/Network/Connection.cs,1\n")))
117+
109118
;;; The identity analysis is intended as a debug aid or to
110119
;;; generate parsed VCS data as input to other tools.
111120
;;; The idea with identity is to dump the parse result to
@@ -144,7 +153,7 @@
144153

145154
(deftest reports-invalid-arguments
146155
(testing "Non-existent input file"
147-
(is (thrown? IllegalArgumentException (app/run "I/do/not/exist"))))
156+
(is (thrown? IllegalArgumentException (app/run "I/do/not/exist" {}))))
148157
(testing "Missing mandatory options (normally added by the front)"
149158
(is (thrown? IllegalArgumentException (app/run svn-log-file {:analysis "coupling"})))))
150159

0 commit comments

Comments
 (0)