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(config): handle mixed relative and absolute paths #427

Merged
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

- fixes a bug where specifying both relative and absolute paths would cause sqlfmt to crash ([#426](https://github.com/tconbeer/sqlfmt/issues/426) - thank you for the issue and fix, [@smcgivern](https://github.com/smcgivern)!)

## [0.18.1] - 2023-05-10

- fixes a bug when lexing `union distinct` tokens ([#417](https://github.com/tconbeer/sqlfmt/issues/417) - thank you, [@paschmaria](https://github.com/paschmaria)!)
Expand Down
2 changes: 1 addition & 1 deletion src/sqlfmt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _get_common_parents(files: List[Path]) -> List[Path]:
assert files, "Must provide a list of paths"
common_parents: Set[Path] = set()
for p in files:
parents = set(p.parents)
parents = set(p.absolute().parents)
if p.is_dir():
parents.add(p)
if not common_parents:
Expand Down
24 changes: 24 additions & 0 deletions tests/unit_tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from pathlib import Path
from typing import Any, List

Expand Down Expand Up @@ -63,6 +64,29 @@ def test_find_config_file_not_in_tree(
assert config_path is None


def test_find_config_file_relative_and_absolute(
tmp_path: Path, files_relpath: List[Path]
) -> None:
# Only check the cases where we are providing more than one path
if len(files_relpath) == 1:
return
Comment on lines +70 to +72
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure there's a better way to do this 😬


current_dir = os.getcwd()
copy_config_file_to_dst("valid_sqlfmt_config.toml", tmp_path)

try:
os.chdir(tmp_path)

files = [tmp_path / files_relpath[0], files_relpath[1]]
search_paths = _get_common_parents(files)
assert tmp_path in search_paths
config_path = _find_config_file(search_paths)
assert config_path
assert config_path == tmp_path / "pyproject.toml"
finally:
os.chdir(current_dir)


def test_load_config_from_path(tmp_path: Path) -> None:
copy_config_file_to_dst("valid_sqlfmt_config.toml", tmp_path)
config = _load_config_from_path(tmp_path / "pyproject.toml")
Expand Down