Skip to content

Commit

Permalink
Merge pull request #44 from 15r10nk/windows-support
Browse files Browse the repository at this point in the history
windows support
  • Loading branch information
15r10nk authored Jan 28, 2024
2 parents 9eda2fb + dcec5e6 commit 29192cc
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 28 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ jobs:
- run: nox --session mypy-${{matrix.python-version}}

test:
runs-on: ubuntu-latest
runs-on: ${{matrix.os}}
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
Expand All @@ -42,7 +43,7 @@ jobs:
run: |
poetry install
poetry run coverage combine
mv .coverage .coverage.${{ matrix.python-version }}
mv .coverage .coverage.${{ matrix.python-version }}.${{matrix.os}}
- name: Upload coverage data
uses: actions/upload-artifact@v3
Expand Down Expand Up @@ -71,6 +72,7 @@ jobs:
env:
TOP: ${{github.workspace}}
run: |
rm .coverage.*.windows-latest
pip install coverage
coverage combine
coverage
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ repos:
stages:
- commit-msg
repo: https://github.com/commitizen-tools/commitizen
rev: 3.12.0
rev: v3.12.0


- repo: https://github.com/PyCQA/docformatter
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v0.6.1 (2024-01-28)

### Fix

- use utf-8 encoding to read and write source files

## v0.6.0 (2023-12-10)

### Feat
Expand Down
2 changes: 1 addition & 1 deletion inline_snapshot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

__all__ = ["snapshot", "external", "outsource"]

__version__ = "0.6.0"
__version__ = "0.6.1"
2 changes: 1 addition & 1 deletion inline_snapshot/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def read_config(path: Path) -> Config:
if not path.exists():
return Config()

data = toml.loads(path.read_text())
data = toml.loads(path.read_text("utf-8"))

result = Config()

Expand Down
3 changes: 2 additions & 1 deletion inline_snapshot/_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def _ensure_directory(self):
gitignore = self.directory / ".gitignore"
if not gitignore.exists():
gitignore.write_text(
"# ignore all snapshots which are not refered in the source\n*-new.*\n"
"# ignore all snapshots which are not refered in the source\n*-new.*\n",
"utf-8",
)

def save(self, name, data):
Expand Down
2 changes: 1 addition & 1 deletion inline_snapshot/_find_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def used_externals_in(source) -> Set[str]:
def used_externals() -> Set[str]:
result = set()
for filename in _inline_snapshot._files_with_snapshots:
result |= used_externals_in(pathlib.Path(filename).read_text())
result |= used_externals_in(pathlib.Path(filename).read_text("utf-8"))

return result

Expand Down
2 changes: 1 addition & 1 deletion inline_snapshot/_rewrite_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def new_code(self):
for lhs, rhs in pairwise(replacements):
assert lhs.range.end <= rhs.range.start

code = self.filename.read_text()
code = self.filename.read_text("utf-8")

is_formatted = code == format_code(code, self.filename)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ license = "MIT"
name = "inline-snapshot"
readme = "README.md"
repository = "https://github.com/15r10nk/inline-snapshots"
version = "0.6.0"
version = "0.6.1"

[tool.poetry.dependencies]
asttokens = ">=2.0.5"
Expand Down
28 changes: 17 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import traceback
from dataclasses import dataclass
from dataclasses import field
from pathlib import Path
from typing import Set

import black
Expand Down Expand Up @@ -61,20 +62,21 @@ def run(self, *flags):
flags = Flags({*flags})

nonlocal filecount
filename = tmp_path / f"test_{filecount}.py"
filename: Path = tmp_path / f"test_{filecount}.py"
filecount += 1

prefix = """\"\"\"
PYTEST_DONT_REWRITE
\"\"\"
# äöß 🐍
from inline_snapshot import snapshot
from inline_snapshot import external
from inline_snapshot import outsource
"""
source = prefix + textwrap.dedent(self.source)

filename.write_text(source)
filename.write_text(source, "utf-8")

print()
print(f'run: inline-snapshot={",".join(flags.to_set())}')
Expand All @@ -91,7 +93,7 @@ def run(self, *flags):
error = False

try:
exec(compile(filename.read_text(), filename, "exec"))
exec(compile(filename.read_text("utf-8"), filename, "exec"))
except AssertionError:
traceback.print_exc()
error = True
Expand All @@ -110,7 +112,7 @@ def run(self, *flags):

recorder.fix_all(tags=["inline_snapshot"])

source = filename.read_text()[len(prefix) :]
source = filename.read_text("utf-8")[len(prefix) :]
print("output:")
print(textwrap.indent(source, " |", lambda line: True).rstrip())
print("reported_flags:", snapshot_flags)
Expand Down Expand Up @@ -172,13 +174,17 @@ def project(pytester):
class Project:
def setup(self, source: str):
self.header = """\
# äöß 🐍
from inline_snapshot import snapshot
from inline_snapshot import outsource
"""
if "# no imports" in source:
self.header = ""
self.header = """\
# äöß 🐍
"""
else:
self.header = """\
# äöß 🐍
from inline_snapshot import snapshot
from inline_snapshot import outsource
"""
Expand All @@ -189,23 +195,23 @@ def setup(self, source: str):
source = header + source
print("write code:")
print(source)
self._filename.write_text(source)
self._filename.write_text(source, "utf-8")

@property
def _filename(self):
return pytester.path / "test_file.py"

def is_formatted(self):
code = self._filename.read_text()
code = self._filename.read_text("utf-8")
return code == format_code(code, self._filename)

def format(self):
self._filename.write_text(
format_code(self._filename.read_text(), self._filename)
format_code(self._filename.read_text("utf-8"), self._filename), "utf-8"
)

def pyproject(self, source):
(pytester.path / "pyproject.toml").write_text(source)
(pytester.path / "pyproject.toml").write_text(source, "utf-8")

def storage(self):
return sorted(
Expand All @@ -216,8 +222,8 @@ def storage(self):

@property
def source(self):
assert self._filename.read_text()[: len(self.header)] == self.header
return self._filename.read_text()[len(self.header) :].lstrip()
assert self._filename.read_text("utf-8")[: len(self.header)] == self.header
return self._filename.read_text("utf-8")[len(self.header) :].lstrip()

def run(self, *args):
cache = pytester.path / "__pycache__"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_docs(project, file, subtests):

header = re.compile("<!-- inline-snapshot:(.*)-->")

text = file.read_text()
text = file.read_text("utf-8")
new_lines = []
block_lines = []
options = set()
Expand Down Expand Up @@ -118,4 +118,4 @@ def test_docs(project, file, subtests):
new_lines.append(line)

if inline_snapshot._inline_snapshot._update_flags.fix: # pragma: no cover
file.write_text("\n".join(new_lines) + "\n")
file.write_text("\n".join(new_lines) + "\n", "utf-8")
10 changes: 6 additions & 4 deletions tests/test_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,14 @@ def test_ensure_imports(tmp_path):
"""\
from os import environ
from os import getcwd
"""
""",
"utf-8",
)

with apply_changes():
ensure_import(file, {"os": ["chdir", "environ"]})

assert file.read_text() == snapshot(
assert file.read_text("utf-8") == snapshot(
"""\
from os import environ
from os import getcwd
Expand All @@ -344,13 +345,14 @@ def test_ensure_imports_with_comment(tmp_path):
file.write_text(
"""\
from os import environ # comment
"""
""",
"utf-8",
)

with apply_changes():
ensure_import(file, {"os": ["chdir"]})

assert file.read_text() == snapshot(
assert file.read_text("utf-8") == snapshot(
"""\
from os import environ # comment
Expand Down
5 changes: 3 additions & 2 deletions tests/test_rewrite_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def test_rewrite(tmp_path):
12345
12345
12345
"""
""",
"utf-8",
)

with ChangeRecorder().activate() as recorder:
Expand All @@ -43,7 +44,7 @@ def test_rewrite(tmp_path):
recorder.fix_all()

assert (
file.read_text()
file.read_text("utf-8")
== """
12a45
1245
Expand Down

0 comments on commit 29192cc

Please sign in to comment.