Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression for files without permissions. #418

Merged
merged 4 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ The versions follow [semantic versioning](https://semver.org).

- Fix faulty file types:
- Extensible Stylesheet Language (`.xsl`) actually uses HTML comment syntax
- Allow creating .license file for write-protected files (#347)
- Allow creating .license file for write-protected files (#347) (#418)
- Make `download` subcommand work correctly outside of project root and with
`--root` (#430)

Expand Down
16 changes: 15 additions & 1 deletion src/reuse/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

import datetime
import logging
import os
import re
import sys
from argparse import ArgumentParser
from gettext import gettext as _
from os import PathLike
from pathlib import Path
from typing import List, NamedTuple, Optional, Sequence
from typing import Iterable, List, NamedTuple, Optional, Sequence

from binaryornot.check import is_binary
from boolean.boolean import ParseError
Expand Down Expand Up @@ -551,6 +552,9 @@ def run(args, project: Project, out=sys.stdout) -> int:

paths = [_determine_license_path(path) for path in args.path]

if not args.explicit_license:
_verify_write_access(paths, args.parser)

# Verify line handling and comment styles before proceeding
if args.style is None and not args.explicit_license:
_verify_paths_line_handling(
Expand Down Expand Up @@ -624,3 +628,13 @@ def run(args, project: Project, out=sys.stdout) -> int:
)

return min(result, 1)


def _verify_write_access(paths: Iterable[PathLike], parser: ArgumentParser):
not_writeable = [
str(path) for path in paths if not os.access(path, os.W_OK)
]
if not_writeable:
parser.error(
_("can't write to '{}'").format("', '".join(not_writeable))
)
24 changes: 24 additions & 0 deletions tests/test_main_addheader.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,30 @@ def test_addheader_explicit_license_doesnt_write_to_file(
assert simple_file.read_text() == "Preserve this"


def test_addheader_to_read_only_file_does_not_traceback(
fake_repository, stringio, mock_date_today
):
"""Trying to add a header without having write permission, shouldn't result
in a traceback. See issue #398"""
_file = fake_repository / "test.sh"
_file.write_text("#!/bin/sh")
_file.chmod(mode=stat.S_IREAD)
with pytest.raises(SystemExit) as info:
main(
[
"addheader",
"--license",
"Apache-2.0",
"--copyright",
"mycorp",
"--style",
"python",
"test.sh",
]
)
assert info.value # should not exit with 0


def test_addheader_license_file(fake_repository, stringio, mock_date_today):
"""Add a header to a .license file if it exists."""
simple_file = fake_repository / "foo.py"
Expand Down