Merge ~marcusboden/juju-lint:bug/1939438 into juju-lint:master

Proposed by Marcus Boden
Status: Merged
Merged at revision: 1a1351f416a008b6dbef2c42872f6881aedd1905
Proposed branch: ~marcusboden/juju-lint:bug/1939438
Merge into: juju-lint:master
Diff against target: 185 lines (+122/-6)
3 files modified
jujulint/config.py (+0/-5)
jujulint/config_default.yaml (+6/-1)
tests/unit/test_config.py (+116/-0)
Reviewer Review Type Date Requested Status
Erhan Sunar (community) Approve
🤖 prod-jenkaas-bootstack continuous-integration Approve
Gabriel Cocenza Approve
Mert Kirpici Pending
BootStack Reviewers Pending
Review via email: mp+433574@code.launchpad.net

Commit message

use values in config file

Values set in $HOME/.config/juju-lint/config.yaml were ignored, due to
the default values of the ArgumentParser. This patch
- removes the default values from the parser
- adds default values to the config_default.yaml
- adds tests to check if the config files are read correctly

Closes-Bug: #1939438

To post a comment you must log in.
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
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
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
Gabriel Cocenza (gabrielcocenza) wrote :

Hi Marcus, Thanks for contributing with the project.

This patch generally LGTM, but I left some comments and suggestions.

I would like to ask you to double check if unit tests [0] are passing because when I cloned your repo, the "test_default_config" is failing because the "config" object is a OrderedDict. Not sure if it's a different version of python, confuse etc.

[0] https://pastebin.canonical.com/p/fFNggCGz9M/

review: Needs Information
Revision history for this message
Marcus Boden (marcusboden) wrote :

> Hi Marcus, Thanks for contributing with the project.
>
> This patch generally LGTM, but I left some comments and suggestions.
>
> I would like to ask you to double check if unit tests [0] are passing because
> when I cloned your repo, the "test_default_config" is failing because the
> "config" object is a OrderedDict. Not sure if it's a different version of
> python, confuse etc.
>
> [0] https://pastebin.canonical.com/p/fFNggCGz9M/

I think it's not the OrderedDict that causes the problem, but the test reading values from ~/.config/juju-lint/config.yaml.
T've changed the test so that it doesn't read that config file anymore.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Erhan Sunar (esunar) wrote :

inline

review: Needs Fixing
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Marcus Boden (marcusboden) wrote :

~gabrielcocenza, here's the output of make test on my machine, as discussed on mattermost:

https://pastebin.canonical.com/p/Ghq4rjD8P2/

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

Thanks Marcus.

Just two little things that I found in the code and we will be ready to go.

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

LGTM

review: Approve
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
Erhan Sunar (esunar) :
review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 1a1351f416a008b6dbef2c42872f6881aedd1905

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/jujulint/config.py b/jujulint/config.py
2index 45c5618..be2453e 100644
3--- a/jujulint/config.py
4+++ b/jujulint/config.py
5@@ -37,7 +37,6 @@ class Config(Configuration):
6 "-l",
7 "--log-level",
8 type=str,
9- default="info",
10 nargs="?",
11 help="The default log level, valid options are info, warn, error or debug",
12 dest="logging.loglevel",
13@@ -46,7 +45,6 @@ class Config(Configuration):
14 "-d",
15 "--output-dir",
16 type=str,
17- default="",
18 metavar="DIR",
19 help=(
20 "Dump gathered cloud state data into %(metavar)s. "
21@@ -67,7 +65,6 @@ class Config(Configuration):
22 self.parser.add_argument(
23 "-c",
24 "--config",
25- default="lint-rules.yaml",
26 help="File to read lint rules from. Defaults to `lint-rules.yaml`",
27 dest="rules.file",
28 )
29@@ -99,7 +96,6 @@ class Config(Configuration):
30 self.parser.add_argument(
31 "--logfile",
32 "-L",
33- default=None,
34 help="File to log to in addition to stdout",
35 dest="logging.file",
36 )
37@@ -107,7 +103,6 @@ class Config(Configuration):
38 "--format",
39 "-F",
40 choices=["text", "json"],
41- default="text",
42 help="Format for output",
43 )
44
45diff --git a/jujulint/config_default.yaml b/jujulint/config_default.yaml
46index 662df79..e9a3bcb 100644
47--- a/jujulint/config_default.yaml
48+++ b/jujulint/config_default.yaml
49@@ -21,8 +21,13 @@
50 # sudo: 'juju-user'
51
52 logging:
53- level: INFO
54+ loglevel: INFO
55 file: jujulint.log
56
57 rules:
58 file: lint-rules.yaml
59+
60+output:
61+ folder:
62+
63+format: text
64diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py
65new file mode 100644
66index 0000000..1d9060d
67--- /dev/null
68+++ b/tests/unit/test_config.py
69@@ -0,0 +1,116 @@
70+#!/usr/bin/python3
71+"""Test correct reading of the all config files."""
72+
73+import os
74+import sys
75+
76+import yaml
77+
78+from jujulint.config import Config
79+
80+config_file = f"{Config().config_dir()}/config.yaml"
81+builtin_open = open
82+builtin_isfile = os.path.isfile
83+
84+mock_file_config = """
85+logging:
86+ loglevel: WARN
87+ file: test.log
88+
89+rules:
90+ file: file-test.yaml
91+
92+
93+output:
94+ folder: folder-test
95+ dump: dump-test
96+
97+format: json
98+"""
99+
100+
101+def patch_user_config(mocker):
102+ """Patch calls to ~/.config/juju-lint/config.yaml.
103+
104+ The confuse package reads the config file in .config, if it exists. We have
105+ to mock the os.isfile call and create a mock open function that only
106+ intercepts calls to that file.
107+ """
108+
109+ def my_mock_open(*args, **kwargs):
110+ if args[0] == config_file:
111+ return mocker.mock_open(read_data=mock_file_config)(*args, **kwargs)
112+ return builtin_open(*args, **kwargs)
113+
114+ def side_effect(filename):
115+ if filename == config_file:
116+ return True
117+ else:
118+ return builtin_isfile(filename)
119+
120+ mocker.patch("os.path.isfile", side_effect=side_effect)
121+ mocker.patch("builtins.open", my_mock_open)
122+
123+
124+def test_config_file(mocker):
125+ """Tests if the config entries set in the .config/juju-lint/config.yaml are correctly applied.
126+
127+ The values in .config/juju-lint/config.yaml should overwrite the fileds in config_default.yaml
128+ """
129+ expected_config = yaml.safe_load(mock_file_config)
130+ patch_user_config(mocker)
131+ config = Config()
132+ # you cannot do config.get(), so we iterate over the toplevel keys
133+ for key in expected_config.keys():
134+ assert config[key].get() == expected_config[key]
135+
136+
137+def test_default_config(mocker):
138+ """Tests if the default values are correctly read from config_default.yaml."""
139+ default_config = {
140+ "logging": {"loglevel": "INFO", "file": "jujulint.log"},
141+ "rules": {"file": "lint-rules.yaml"},
142+ "output": {"folder": None},
143+ "format": "text",
144+ }
145+
146+ # Don't use the .config/juju-lint/config.yaml!
147+ def side_effect(filename):
148+ if filename == config_file:
149+ return False
150+ else:
151+ return builtin_isfile(filename)
152+
153+ mocker.patch("os.path.isfile", side_effect=side_effect)
154+
155+ config = Config()
156+ for key in default_config.keys():
157+ assert config[key].get() == default_config[key]
158+
159+
160+def test_parser_options(mocker):
161+ """Tests if cli options overwrite the options in config files."""
162+ cli_config = {
163+ "logging": {"loglevel": "DEBUG", "file": "cli.log"},
164+ "rules": {"file": "cli-rules.yaml"},
165+ "output": {"folder": "cli/folder"},
166+ "format": "json",
167+ }
168+ test_args = sys.argv + [
169+ "-F",
170+ cli_config["format"],
171+ "-l",
172+ cli_config["logging"]["loglevel"],
173+ "-d",
174+ cli_config["output"]["folder"],
175+ "-c",
176+ cli_config["rules"]["file"],
177+ "-L",
178+ cli_config["logging"]["file"],
179+ ]
180+
181+ mocker.patch.object(sys, "argv", test_args)
182+ patch_user_config(mocker)
183+ config = Config()
184+ for key in cli_config.keys():
185+ assert config[key].get() == cli_config[key]

Subscribers

People subscribed via source and target branches