Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, pytest_request: FixtureRequest, **kwargs) -> None:
"""Create the root dir and make it the current dir (needed by NitpickChecker)."""
subdir = "/".join(pytest_request.module.__name__.split(".")[1:])
caller_function_name = pytest_request.node.name
self.root_dir = TEMP_ROOT_PATH / subdir / caller_function_name # type: Path
# To make debugging of mock projects easy, each test should not reuse another test directory.
self.root_dir.mkdir(parents=True)
self.cache_dir = self.root_dir / CACHE_DIR_NAME / PROJECT_NAME
self.files_to_lint = [] # type: List[Path]
if kwargs.get("setup_py", True):
self.save_file("setup.py", "x = 1")
def test_flag_format_env_variable():
"""Test flag formatting and env variable."""
class OtherFlags(Enum):
"""Some flags to be used on the assertions below."""
MULTI_WORD = 1
SOME_OPTION = 2
assert NitpickApp.format_flag(OtherFlags.MULTI_WORD) == "--nitpick-multi-word"
os.environ["NITPICK_SOME_OPTION"] = "something"
assert NitpickApp.format_env(OtherFlags.SOME_OPTION) == "NITPICK_SOME_OPTION"
assert NitpickApp.get_env(OtherFlags.SOME_OPTION) == "something"
assert NitpickApp.get_env(OtherFlags.MULTI_WORD) == ""
def assert_merged_style(self, toml_string: str):
"""Assert the contents of the merged style file."""
expected = TOMLFormat(path=self.cache_dir / MERGED_STYLE_TOML)
actual = TOMLFormat(string=dedent(toml_string))
compare(expected.as_data, actual.as_data)
def assert_merged_style(self, toml_string: str):
"""Assert the contents of the merged style file."""
expected = TOMLFormat(path=self.cache_dir / MERGED_STYLE_TOML)
actual = TOMLFormat(string=dedent(toml_string))
compare(expected.as_data, actual.as_data)
def fetch_style_from_url(self, url: str) -> Optional[Path]:
"""Fetch a style file from a URL, saving the contents in the cache dir."""
if NitpickApp.current().offline:
# No style will be fetched in offline mode
return None
if self._first_full_path and not is_url(url):
prefix, rest = self._first_full_path.split(":/")
domain_plus_url = str(rest).strip("/").rstrip("/") + "/" + url
new_url = "{}://{}".format(prefix, domain_plus_url)
else:
new_url = url
parsed_url = list(urlparse(new_url))
if not parsed_url[2].endswith(TOML_EXTENSION):
parsed_url[2] += TOML_EXTENSION
new_url = urlunparse(parsed_url)
if new_url in self._already_included:
def format_hook(expected_dict) -> str:
"""Format the hook so it's easy to copy and paste it to the .yaml file: ID goes first, indent with spaces."""
lines = YAMLFormat(data=expected_dict).reformatted
output = [] # type: List[str]
for line in lines.split("\n"):
if line.startswith("id:"):
output.insert(0, " - {}".format(line))
else:
output.append(" {}".format(line))
return "\n".join(output)
if not parsed_url[2].endswith(TOML_EXTENSION):
parsed_url[2] += TOML_EXTENSION
new_url = urlunparse(parsed_url)
if new_url in self._already_included:
return None
if not NitpickApp.current().cache_dir:
raise FileNotFoundError("Cache dir does not exist")
try:
response = requests.get(new_url)
except requests.ConnectionError:
click.secho(
"Your network is unreachable. Fix your connection or use {} / {}=1".format(
NitpickApp.format_flag(NitpickApp.Flags.OFFLINE), NitpickApp.format_env(NitpickApp.Flags.OFFLINE)
),
fg="red",
err=True,
)
return None
if not response.ok:
raise FileNotFoundError("Error {} fetching style URL {}".format(response, new_url))
# Save the first full path to be used by the next files without parent.
if not self._first_full_path:
self._first_full_path = new_url.rsplit("/", 1)[0]
contents = response.text
style_path = NitpickApp.current().cache_dir / "{}.toml".format(slugify(new_url))
NitpickApp.current().cache_dir.mkdir(parents=True, exist_ok=True)
style_path.write_text(contents)
def cmd_fixby(args):
load_db()
if db.change_issue(args.issue, 'Fix_By', args.newfixby):
if not args.no_commit:
return config.vcs.commit()
else:
return True
else:
return False
def cmd_component(args):
load_db()
if db.change_issue(args.issue, 'Component', args.newcomponent):
if not args.no_commit:
return config.vcs.commit()
else:
return True
else:
return False
fulluser = ''
for row in config.users:
if args.newowner in row:
if fulluser != '':
print "Ambiguous user. Please be more specific"
return False
else:
fulluser = row
if fulluser == '':
print "Unknown user"
return False
if db.change_issue(args.issue, 'Owner', fulluser):
if not args.no_commit:
return config.vcs.commit()
else:
return True
else:
return False