Skip to content

Commit

Permalink
fix: set the file mode for template file permissions when merging a f…
Browse files Browse the repository at this point in the history
…ile (#784)

Fixes #783
  • Loading branch information
chingor13 authored Oct 6, 2020
1 parent 0c868d4 commit 487eba7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions synthtool/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def _merge_file(

final_text = merge(source_text, dest_text, dest_path)

# use the source file's file permission mode
os.chmod(dest_path, os.stat(source_path).st_mode)
if final_text != dest_text:
dest_file.seek(0)
dest_file.write(final_text)
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/executable_file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

echo "test executable file"
34 changes: 33 additions & 1 deletion tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def expand_path_fixtures(tmpdir):
"dira/e.txt": "epsilon text",
"dira/f.py": "eff python",
"dirb/suba/g.py": "gamma python",
"executable_file.sh": "file that should be executable but is not",
}

for file_name, content in files.items():
Expand Down Expand Up @@ -64,7 +65,7 @@ def executable_fixtures(tmpdir):
["input", "expected"],
[
("a.txt", ["a.txt"]),
("*", ["a.txt", "b.py", "c.md", "dira", "dirb"]),
("*", ["a.txt", "b.py", "c.md", "dira", "dirb", "executable_file.sh"]),
("*.py", ["b.py"]),
("**/*.py", ["b.py", normpath("dira/f.py"), normpath("dirb/suba/g.py")]),
(
Expand All @@ -79,6 +80,7 @@ def executable_fixtures(tmpdir):
"dirb",
normpath("dirb/suba"),
normpath("dirb/suba/g.py"),
"executable_file.sh",
],
),
],
Expand Down Expand Up @@ -112,6 +114,7 @@ def test__filter_files(expand_path_fixtures):
normpath("dira/e.txt"),
normpath("dira/f.py"),
normpath("dirb/suba/g.py"),
"executable_file.sh",
]


Expand Down Expand Up @@ -147,6 +150,7 @@ def test__move_to_dest(expand_path_fixtures):
"dest/dirb",
"dest/dirb/suba",
"dest/dirb/suba/g.py",
"dest/executable_file.sh",
]
]

Expand Down Expand Up @@ -221,3 +225,31 @@ def test_required_move_not_found():
assert False, "should have thrown error"
except transforms.MissingSourceError:
assert True


def _noop_merge(source_text: str, destination_text: str, file_path: Path) -> str:
return source_text


def test_copy_with_merge_file_permissions(expand_path_fixtures):
destination_file = expand_path_fixtures / "executable_file.sh"
template_directory = Path(__file__).parent / "fixtures"
_tracked_paths.add(template_directory)
template = template_directory / "executable_file.sh"

# ensure that the destination existing file already has incorrect correct
# file permissions
assert os.path.exists(destination_file)
assert os.stat(destination_file).st_mode != os.stat(template).st_mode

# Move to a different dir to make sure that move doesn't depend on the cwd
os.chdir(tempfile.gettempdir())
transforms.move(
sources=template_directory / "executable_file.sh",
destination=expand_path_fixtures / "executable_file.sh",
merge=_noop_merge,
required=True,
)

# ensure that the destination existing file now has the correct file permissions
assert os.stat(destination_file).st_mode == os.stat(template).st_mode

0 comments on commit 487eba7

Please sign in to comment.