Skip to content

Commit

Permalink
feat: add qmlformat script
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwu1105 committed Sep 6, 2023
1 parent d7480c6 commit 148e269
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/.coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ omit =
scripts/lupdate.py
scripts/linguist.py
scripts/lrelease.py
scripts/qmlformat.py

[report]
fail_under = 100
Expand Down
21 changes: 21 additions & 0 deletions python/scripts/qmlformat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# pylint: disable=import-error,ungrouped-imports

import sys

from utils import is_installed, parse_qt_dependency

if __name__ == "__main__":
dep = parse_qt_dependency()
if dep == "PySide6":
from PySide6.scripts.pyside_tool import qmlformat

elif is_installed("PySide6"):
from PySide6.scripts.pyside_tool import qmlformat
else:
ERR_MSG = (
"No qmlformat can be found in the current Python environment. "
"Make sure the latest PySide6 is installed. "
"Update configuration to disable qmlformat integration if you don't need it."
)
sys.exit(ERR_MSG)
sys.exit(qmlformat())
10 changes: 10 additions & 0 deletions python/tests/assets/qml/unformatted.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import QtQuick
Rectangle {
width: 200
height: 100
color: "red"


Text{anchors.centerIn: parent
text: "Hello, World!"
}}
82 changes: 82 additions & 0 deletions python/tests/test_qmlformat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import os

import pytest

from scripts.utils import SupportedQtDependencies
from tests import ASSETS_DIR, filter_available_qt_dependencies, invoke_script


@pytest.mark.parametrize(
"qt_dependency",
filter_available_qt_dependencies(["PySide6"]),
)
def test_qmlformat_version(qt_dependency: SupportedQtDependencies):
result = invoke_script("qmlformat", ["--version"], qt_dependency)

assert result.returncode == 0
assert len(result.stdout.decode("utf-8")) > 0


@pytest.mark.parametrize(
"qt_dependency",
filter_available_qt_dependencies(["PySide6"]),
)
def test_qmlformat_format_file(qt_dependency: SupportedQtDependencies):
filename = "unformatted.qml"
reset_unformatted_qml_file(filename)

result = invoke_script(
"qmlformat", [get_assets_path(filename), "--inplace"], qt_dependency
)

assert result.returncode == 0

with open(get_assets_path(filename), "r", encoding="utf-8") as file:
assert file.read() == (
"import QtQuick\n"
"\n"
"Rectangle {\n"
" width: 200\n"
" height: 100\n"
' color: "red"\n'
"\n"
" Text {\n"
" anchors.centerIn: parent\n"
' text: "Hello, World!"\n'
" }\n"
"}\n"
)

reset_unformatted_qml_file(filename)


def reset_unformatted_qml_file(filename: str):
with open(get_assets_path(filename), "w", encoding="utf-8") as file:
file.write(
"import QtQuick\n"
"Rectangle {\n"
"width: 200\n"
"height: 100\n"
'color: "red"\n'
" \n"
" \n"
" Text{anchors.centerIn: parent\n"
'text: "Hello, World!"\n'
"}}\n"
)


@pytest.mark.parametrize(
"qt_dependency",
filter_available_qt_dependencies(["PySide6"]),
)
def test_qmlformat_format_invalid_file(qt_dependency: SupportedQtDependencies):
filename = "syntax_error.qml"

result = invoke_script("qmlformat", [get_assets_path(filename)], qt_dependency)

assert result.returncode != 0


def get_assets_path(filename: str) -> str:
return os.path.join(ASSETS_DIR, "qml", filename)

0 comments on commit 148e269

Please sign in to comment.