Merge ~gabrielcocenza/juju-lint:bug/1978739 into juju-lint:master

Proposed by Gabriel Cocenza
Status: Merged
Approved by: Martin Kalcok
Approved revision: 5160caab5760de907af591d05de6217405ef989a
Merged at revision: ea3f3020a6e0d77cdb4ffc7f29d7f7ff3b9b112d
Proposed branch: ~gabrielcocenza/juju-lint:bug/1978739
Merge into: juju-lint:master
Diff against target: 450 lines (+144/-58)
19 files modified
.pre-commit-config.yaml (+19/-0)
CONTRIBUTING.md (+14/-5)
Makefile (+48/-7)
contrib/fcb-stein-bionic.yaml (+1/-1)
contrib/fcb-ussuri-bionic-ovs.yaml (+1/-1)
contrib/fcb-ussuri-bionic.yaml (+1/-1)
contrib/fcb-ussuri-focal-ovs.yaml (+1/-1)
contrib/fcb-ussuri-focal.yaml (+1/-1)
contrib/fcb-yoga-focal.yaml (+1/-1)
contrib/includes/aggregator-kubernetes.yaml (+16/-0)
contrib/includes/release/stein.yaml (+1/-1)
contrib/includes/release/ussuri.yaml (+1/-1)
contrib/includes/release/yoga.yaml (+1/-1)
contrib/includes/series/focal.yaml (+1/-1)
contrib/kubernetes.yaml (+1/-17)
setup.py (+1/-1)
tests/requirements.txt (+2/-0)
tests/test_jujulint.py (+5/-0)
tox.ini (+28/-18)
Reviewer Review Type Date Requested Status
Martin Kalcok (community) Approve
Eric Chen Approve
Review via email: mp+426219@code.launchpad.net

Commit message

applied pattern of bootstack charms for make and tox commands.

- added pre-commit support.
- solved lint issues.
- removed debug flag to build snap. If the build fails, it should
  stop the CI and not open the shell from the snap.
- added an aggregator-kubernetes to solve pre-commit yaml check.
- changed tox and make commands to match scripts on the bootstack CI.

Closes-Bug: #1978739
Partial-Bug: #1978841

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Eric Chen (eric-chen) :
review: Approve
Revision history for this message
Martin Kalcok (martin-kalcok) wrote :

Thanks for the cleanup effort. It's definitely needed. However I have two comments:

* We were talking with the rest of the bootstack that we won't be forcing pyptoject.toml into new charms/projects just yet. We already have tox and Makefiles and adding pyptoject on top seems unnecessary.

* pre-commits should use tox to execute actions to ensure that the same versions of tools (e.g. black, flake8) will be used as in the CI pipeline. Example of such pre-commit (by Robert Gildein): https://pastebin.canonical.com/p/gBjX3qqfkY/

review: Needs Fixing
Revision history for this message
Gabriel Cocenza (gabrielcocenza) wrote :

Thanks for your review Martin. I've removed the pyptoject.toml and changed the pre-commit as you said, to use the same environment of tox.

Revision history for this message
Martin Kalcok (martin-kalcok) wrote :

One small question in-line, otherwise LGTM.

Revision history for this message
Gabriel Cocenza (gabrielcocenza) :
Revision history for this message
Martin Kalcok (martin-kalcok) :
review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision ea3f3020a6e0d77cdb4ffc7f29d7f7ff3b9b112d

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
2new file mode 100644
3index 0000000..c327de3
4--- /dev/null
5+++ b/.pre-commit-config.yaml
6@@ -0,0 +1,19 @@
7+repos:
8+ - repo: https://github.com/pre-commit/pre-commit-hooks
9+ rev: v4.0.1
10+ hooks:
11+ - id: check-executables-have-shebangs
12+ - id: check-merge-conflict
13+ - id: end-of-file-fixer
14+ - id: trailing-whitespace
15+ - id: check-added-large-files
16+ - id: check-json
17+ - id: check-yaml
18+ args: ["--unsafe"]
19+ - repo: local
20+ hooks:
21+ - id: lint
22+ name: lint
23+ entry: tox -e lint
24+ language: system
25+ types: [ python ]
26diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
27index 7e46600..1e5c9b9 100644
28--- a/CONTRIBUTING.md
29+++ b/CONTRIBUTING.md
30@@ -22,11 +22,17 @@ git clone git+ssh://<LAUNCHPAD_USER>@git.launchpad.net/juju-lint
31 cd juju-lint/
32 ```
33
34-You can use the environments created by `tox` for development:
35+Create and activate a virtualenv with the development requirements:
36
37 ```shell
38-tox --notest -e unit
39-source .tox/unit/bin/activate
40+make dev-environment
41+```
42+
43+This step is mandatory, otherwise a message like this will appear if you try to commit:
44+
45+```shell
46+$ git commit
47+`pre-commit` not found. Did you forget to activate your virtualenv?
48 ```
49
50 After making your changes you can run the CI without the need of building and installing the snap by using:
51@@ -40,9 +46,12 @@ python3 -m jujulint.cli <PATH_TO_YAML> -c <PATH_TO_RULE_FILE> -l debug
52
53 ```shell
54 make lint # check code style
55-make test # unit tests
56+make reformat # reformat the code using black and isort
57+make unittests # run unit tests
58+make functional # run functional tests
59+make test # run lint, unittests and functional
60 make build # build the snap
61-make clean # clean the snapcraft lxd containers
62+make clean # clean the snapcraft lxd containers and the snap files created
63 ```
64
65 ## Canonical Contributor Agreement
66diff --git a/Makefile b/Makefile
67index 452acd4..49303b3 100644
68--- a/Makefile
69+++ b/Makefile
70@@ -1,15 +1,56 @@
71+help:
72+ @echo "This project supports the following targets"
73+ @echo ""
74+ @echo " make help - show this text"
75+ @echo " make clean - remove unneeded files"
76+ @echo " make dev-environment - setup the development environment"
77+ @echo " make build - build the snap"
78+ @echo " make lint - run flake8, black --check and isort --check-only"
79+ @echo " make reformat - run black and isort and reformat files"
80+ @echo " make unittests - run the tests defined in the unittest subdirectory"
81+ @echo " make functional - run the tests defined in the functional subdirectory"
82+ @echo " make test - run lint, proof, unittests and functional targets"
83+ @echo " make pre-commit - run pre-commit checks on all the files"
84+ @echo ""
85+
86+
87 lint:
88- tox -e lintverbose
89+ @echo "Running lint checks"
90+ @tox -e lint
91+
92+unittests:
93+ @echo "Running unit tests"
94+ @tox -e unit
95
96-test:
97- tox -e unit
98+test: lint unittests functional
99+ @echo "Tests completed for the snap."
100
101-format-code:
102- tox -e format-code
103+reformat:
104+ @echo "Reformat files with black and isort"
105+ tox -e reformat
106
107 build:
108- snapcraft --use-lxd --debug
109+ @echo "Building the snap"
110+ @snapcraft --use-lxd
111
112 clean:
113- snapcraft clean --use-lxd
114+ @echo "Cleaning snap"
115+ @snapcraft clean --use-lxd
116+ @echo "Cleaning existing snap builds"
117+ @find . -name "*.snap" -delete
118+
119+dev-environment:
120+ @echo "Creating virtualenv and installing pre-commit"
121+ @virtualenv -p python3 .venv
122+ @.venv/bin/pip install -r tests/requirements.txt
123+ @.venv/bin/pre-commit install
124+
125+functional: build
126+ @echo "Executing functional tests using built snap"
127+ @tox -e func
128+
129+pre-commit:
130+ @tox -e pre-commit
131
132+# The targets below don't depend on a file
133+.PHONY: help clean dev-environment build lint reformat unittests functional test pre-commit
134diff --git a/contrib/fcb-stein-bionic.yaml b/contrib/fcb-stein-bionic.yaml
135index 863058f..6d9f16f 100644
136--- a/contrib/fcb-stein-bionic.yaml
137+++ b/contrib/fcb-stein-bionic.yaml
138@@ -6,4 +6,4 @@
139 !include includes/openstack.yaml
140 !include includes/operations.yaml
141 !include includes/saas.yaml
142-!include includes/aggregator-openstack.yaml
143\ No newline at end of file
144+!include includes/aggregator-openstack.yaml
145diff --git a/contrib/fcb-ussuri-bionic-ovs.yaml b/contrib/fcb-ussuri-bionic-ovs.yaml
146index 51de731..86d624d 100644
147--- a/contrib/fcb-ussuri-bionic-ovs.yaml
148+++ b/contrib/fcb-ussuri-bionic-ovs.yaml
149@@ -6,4 +6,4 @@
150 !include includes/openstack.yaml
151 !include includes/operations.yaml
152 !include includes/saas.yaml
153-!include includes/aggregator-openstack.yaml
154\ No newline at end of file
155+!include includes/aggregator-openstack.yaml
156diff --git a/contrib/fcb-ussuri-bionic.yaml b/contrib/fcb-ussuri-bionic.yaml
157index 6fade6b..76e899b 100644
158--- a/contrib/fcb-ussuri-bionic.yaml
159+++ b/contrib/fcb-ussuri-bionic.yaml
160@@ -6,4 +6,4 @@
161 !include includes/openstack.yaml
162 !include includes/operations.yaml
163 !include includes/saas.yaml
164-!include includes/aggregator-openstack.yaml
165\ No newline at end of file
166+!include includes/aggregator-openstack.yaml
167diff --git a/contrib/fcb-ussuri-focal-ovs.yaml b/contrib/fcb-ussuri-focal-ovs.yaml
168index a29ed0e..0e989a0 100644
169--- a/contrib/fcb-ussuri-focal-ovs.yaml
170+++ b/contrib/fcb-ussuri-focal-ovs.yaml
171@@ -6,4 +6,4 @@
172 !include includes/openstack.yaml
173 !include includes/operations.yaml
174 !include includes/saas.yaml
175-!include includes/aggregator-openstack.yaml
176\ No newline at end of file
177+!include includes/aggregator-openstack.yaml
178diff --git a/contrib/fcb-ussuri-focal.yaml b/contrib/fcb-ussuri-focal.yaml
179index 2e6f2eb..8b53cfe 100644
180--- a/contrib/fcb-ussuri-focal.yaml
181+++ b/contrib/fcb-ussuri-focal.yaml
182@@ -6,4 +6,4 @@
183 !include includes/openstack.yaml
184 !include includes/operations.yaml
185 !include includes/saas.yaml
186-!include includes/aggregator-openstack.yaml
187\ No newline at end of file
188+!include includes/aggregator-openstack.yaml
189diff --git a/contrib/fcb-yoga-focal.yaml b/contrib/fcb-yoga-focal.yaml
190index 4293a7d..632a73a 100644
191--- a/contrib/fcb-yoga-focal.yaml
192+++ b/contrib/fcb-yoga-focal.yaml
193@@ -6,4 +6,4 @@
194 !include includes/openstack.yaml
195 !include includes/operations.yaml
196 !include includes/saas.yaml
197-!include includes/aggregator-openstack.yaml
198\ No newline at end of file
199+!include includes/aggregator-openstack.yaml
200diff --git a/contrib/includes/aggregator-kubernetes.yaml b/contrib/includes/aggregator-kubernetes.yaml
201new file mode 100644
202index 0000000..95c2099
203--- /dev/null
204+++ b/contrib/includes/aggregator-kubernetes.yaml
205@@ -0,0 +1,16 @@
206+operations charms: &operations-charms
207+ - *operations-kubernetes-mandatory-charms
208+ - *operations-mandatory-charms
209+ - *operations-mandatory-deps
210+ - *operations-mandatory-subs
211+ - *operations-optional-charms
212+ - *operations-optional-subs
213+
214+kubernetes charms: &kubernetes-charms
215+ - *kubernetes-mandatory-charms
216+ - *kubernetes-optional-charms
217+
218+known charms:
219+ - ubuntu
220+ - *operations-charms
221+ - *kubernetes-charms
222diff --git a/contrib/includes/release/stein.yaml b/contrib/includes/release/stein.yaml
223index 7319287..3195046 100644
224--- a/contrib/includes/release/stein.yaml
225+++ b/contrib/includes/release/stein.yaml
226@@ -1,4 +1,4 @@
227 # add prometheus ceph exporter if openstack version is lesser than ussuri
228
229 operations openstack mandatory release: &operations-openstack-mandatory-release
230- - prometheus-ceph-exporter
231\ No newline at end of file
232+ - prometheus-ceph-exporter
233diff --git a/contrib/includes/release/ussuri.yaml b/contrib/includes/release/ussuri.yaml
234index ddf9beb..4080368 100644
235--- a/contrib/includes/release/ussuri.yaml
236+++ b/contrib/includes/release/ussuri.yaml
237@@ -2,4 +2,4 @@
238 # Instead, use ceph-dashboard
239
240 operations openstack mandatory release: &operations-openstack-mandatory-release
241- - ceph-dashboard
242\ No newline at end of file
243+ - ceph-dashboard
244diff --git a/contrib/includes/release/yoga.yaml b/contrib/includes/release/yoga.yaml
245index ddf9beb..4080368 100644
246--- a/contrib/includes/release/yoga.yaml
247+++ b/contrib/includes/release/yoga.yaml
248@@ -2,4 +2,4 @@
249 # Instead, use ceph-dashboard
250
251 operations openstack mandatory release: &operations-openstack-mandatory-release
252- - ceph-dashboard
253\ No newline at end of file
254+ - ceph-dashboard
255diff --git a/contrib/includes/series/focal.yaml b/contrib/includes/series/focal.yaml
256index 4084113..4771d14 100644
257--- a/contrib/includes/series/focal.yaml
258+++ b/contrib/includes/series/focal.yaml
259@@ -1,3 +1,3 @@
260 openstack loadbalancer: &openstack-loadbalancer
261 - openstack-loadbalancer
262-operations openstack mandatory series: &operations-openstack-mandatory-series []
263\ No newline at end of file
264+operations openstack mandatory series: &operations-openstack-mandatory-series []
265diff --git a/contrib/kubernetes.yaml b/contrib/kubernetes.yaml
266index 27eeba6..4e90b94 100644
267--- a/contrib/kubernetes.yaml
268+++ b/contrib/kubernetes.yaml
269@@ -4,20 +4,4 @@
270 !include includes/operations.yaml
271 !include includes/kubernetes.yaml
272 !include includes/saas.yaml
273-
274-operations charms: &operations-charms
275- - *operations-kubernetes-mandatory-charms
276- - *operations-mandatory-charms
277- - *operations-mandatory-deps
278- - *operations-mandatory-subs
279- - *operations-optional-charms
280- - *operations-optional-subs
281-
282-kubernetes charms: &kubernetes-charms
283- - *kubernetes-mandatory-charms
284- - *kubernetes-optional-charms
285-
286-known charms:
287- - ubuntu
288- - *operations-charms
289- - *kubernetes-charms
290+!include includes/aggregator-kubernetes.yaml
291diff --git a/setup.py b/setup.py
292index b6b3dc9..9314d60 100644
293--- a/setup.py
294+++ b/setup.py
295@@ -16,9 +16,9 @@
296 # You should have received a copy of the GNU General Public License
297 # along with this program. If not, see <http://www.gnu.org/licenses/>.
298 """Setuptools packaging metadata for juju-lint."""
299+import warnings
300
301 import setuptools
302-import warnings
303
304 warnings.simplefilter("ignore", UserWarning) # Older pips complain about newer options.
305
306diff --git a/tests/requirements.txt b/tests/requirements.txt
307index 2575eb3..d31d3fb 100644
308--- a/tests/requirements.txt
309+++ b/tests/requirements.txt
310@@ -1,5 +1,6 @@
311 # Module requirements
312 black
313+colorama
314 flake8
315 flake8-colors
316 flake8-docstrings
317@@ -13,3 +14,4 @@ pytest
318 pytest-cov
319 pytest-html
320 pytest-mock
321+pre-commit
322diff --git a/tests/test_jujulint.py b/tests/test_jujulint.py
323index e99401a..6e40b74 100644
324--- a/tests/test_jujulint.py
325+++ b/tests/test_jujulint.py
326@@ -787,6 +787,7 @@ applications:
327 }
328
329 def test_check_spaces_detect_mismatches(self, linter, mocker):
330+ """Test that check spaces mismatch gives warning message."""
331 mock_log: mock.MagicMock = mocker.patch("jujulint.lint.Linter._log_with_header")
332 linter.model.app_to_charm = self.check_spaces_example_app_charm_map
333
334@@ -805,6 +806,7 @@ applications:
335 )
336
337 def test_check_spaces_enforce_endpoints(self, linter):
338+ """Test that check spaces enforce endpoints."""
339 linter.model.app_to_charm = self.check_spaces_example_app_charm_map
340
341 # Run the space check with prometheus:target endpoint enforced.
342@@ -824,6 +826,7 @@ applications:
343 assert len(errors) == 2
344
345 def test_check_spaces_enforce_relations(self, linter):
346+ """Test that check spaces enforce relations."""
347 linter.model.app_to_charm = self.check_spaces_example_app_charm_map
348
349 # Run the space check with prometheus:target endpoint enforced.
350@@ -845,6 +848,7 @@ applications:
351 assert len(errors) == 2
352
353 def test_check_spaces_ignore_endpoints(self, linter, mocker):
354+ """Test that check spaces can ignore endpoints."""
355 mock_log: mock.MagicMock = mocker.patch("jujulint.lint.Linter._log_with_header")
356 linter.model.app_to_charm = self.check_spaces_example_app_charm_map
357
358@@ -867,6 +871,7 @@ applications:
359 assert mock_log.call_count == 0
360
361 def test_check_spaces_ignore_relations(self, linter, mocker):
362+ """Test that check spaces can ignore relations."""
363 mock_log: mock.MagicMock = mocker.patch("jujulint.lint.Linter._log_with_header")
364 linter.model.app_to_charm = self.check_spaces_example_app_charm_map
365
366diff --git a/tox.ini b/tox.ini
367index 843a442..40349c8 100644
368--- a/tox.ini
369+++ b/tox.ini
370@@ -4,14 +4,15 @@ exclude =
371 __pycache__,
372 .tox,
373 .eggs,
374+ .venv,
375 venv,
376 max-line-length = 120
377 max-complexity = 10
378-ignore = C901
379+ignore = D100, D103, C901
380
381 [tox]
382 skipsdist=True
383-envlist = lintverbose, unit
384+envlist = lint, unit, func
385 skip_missing_interpreters = True
386
387 [testenv]
388@@ -19,9 +20,6 @@ basepython = python3.8
389 deps =
390 -r{toxinidir}/tests/requirements.txt
391 -r{toxinidir}/requirements.txt
392-commands =
393- lint: flake8 {toxinidir}
394- test: python3 -m unittest discover tests
395
396 [testenv:unit]
397 commands =
398@@ -38,27 +36,39 @@ commands =
399 setenv = PYTHONPATH={toxinidir}/lib
400
401 [testenv:lint]
402-commands = flake8 jujulint --format=html --htmldir=tests/report/lint/ --tee
403-
404-[testenv:lintverbose]
405 commands =
406- flake8 {toxinidir}/jujulint {toxinidir}/tests
407- black --check {toxinidir}/jujulint {toxinidir}/tests
408- isort --check {toxinidir}/jujulint {toxinidir}/tests
409+ flake8 --color always
410+ black --check --diff --color .
411+ isort --check-only --diff --color .
412
413
414-[testenv:format-code]
415+[testenv:reformat]
416 commands =
417- black {toxinidir}/jujulint {toxinidir}/tests
418- isort {toxinidir}/jujulint {toxinidir}/tests
419-
420+ black .
421+ isort .
422
423-[testenv:lintjunit]
424-commands = flake8 jujulint --format junit-xml --output-file=report/lint/junit.xml
425+[testenv:func]
426+#TODO(gabrielcocenza) add func tests
427
428 [pytest]
429-filterwarnings =
430+filterwarnings =
431 ignore::DeprecationWarning
432
433 [isort]
434 profile = black
435+skip_glob =
436+ .eggs,
437+ __pycache__,
438+ .git,
439+ .tox,
440+ .venv,
441+ .build,
442+ venv,
443+ dist,
444+ mod,
445+ build,
446+
447+[testenv:pre-commit]
448+deps = pre-commit
449+commands =
450+ pre-commit run -a

Subscribers

People subscribed via source and target branches