Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
XFY9326 committed Feb 26, 2024
1 parent bb9fe27 commit ce93bd5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install Protoc
uses: arduino/setup-protoc@v3
- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel setuptools
pip install pytest hatch versioningit
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Build protobuf
run: |
python ./build_protobuf.py
- name: Build dist
run: |
hatch build
Expand Down
9 changes: 7 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install Protoc
uses: arduino/setup-protoc@v3
- name: Install dependencies
run: |
python -m pip install --upgrade pip wheel setuptools
pip install pytest pytest-cov
pip install pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Build protobuf
run: |
python ./build_protobuf.py
- name: Test with pytest
run: |
pytest --cov=src ./tests/
pytest ./tests/
19 changes: 12 additions & 7 deletions build_protobuf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from pathlib import Path

PROJECT_ROOT = Path(__file__).parent
PROJECT_ROOT = Path(__file__).parent.absolute()

PROTOC_PATH = "protoc"
PROTO_DIR = PROJECT_ROOT.joinpath("proto")
Expand All @@ -12,17 +12,22 @@
print("protoc not found")
exit(1)

old_files = [i for i in os.listdir(PYTHON_OUTPUT_DIR) if i.endswith(("_pb2.py", "_pb2.pyi"))]
if len(old_files) > 0:
print("Removing old protobuf files")
for file in old_files:
os.remove(PYTHON_OUTPUT_DIR.joinpath(file))
if PYTHON_OUTPUT_DIR.exists():
old_files = [i for i in os.listdir(PYTHON_OUTPUT_DIR) if i.endswith(("_pb2.py", "_pb2.pyi"))]
if len(old_files) > 0:
print("Removing old protobuf files")
for file in old_files:
os.remove(PYTHON_OUTPUT_DIR.joinpath(file))
else:
print("Creating proto package")
PYTHON_OUTPUT_DIR.mkdir()
PYTHON_OUTPUT_DIR.joinpath("__init__.py").touch()

print("Creating protobuf files")
os.system(" ".join([
str(PROTOC_PATH),
f"--proto_path=\"{PROTO_DIR}\"",
f"--pyi_out=\"{PYTHON_OUTPUT_DIR}\"",
f"--python_out=\"{PYTHON_OUTPUT_DIR}\"",
f"\"{PROTO_DIR}/*\""
f"\"{PROTO_DIR.joinpath('*')}\""
]))
6 changes: 3 additions & 3 deletions src/xiaomi_ndef/nfc.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def decode(self, data: bytes) -> NfcTagAppData:
return NfcTagAppData.decode(BytesIO(data))

def __str__(self) -> str:
return "V1"
return self.__repr__()

def __repr__(self) -> str:
return "V1"
Expand All @@ -57,7 +57,7 @@ def decode(self, data: bytes) -> NfcTagAppData:
return NfcTagAppData.decode(BytesIO(data))

def __str__(self) -> str:
return "V2"
return self.__repr__()

def __repr__(self) -> str:
return "V2"
Expand All @@ -71,7 +71,7 @@ def decode(self, data: bytes) -> HandoffAppData:
return HandoffAppData.decode(BytesIO(data))

def __str__(self) -> str:
return "Handoff"
return self.__repr__()

def __repr__(self) -> str:
return "Handoff"
Expand Down
3 changes: 3 additions & 0 deletions tests/test_xiaomi_ndef.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,19 @@ def test_v1_protocol(self) -> None:
payload = self._test_protocol(nfc.V1NfcProtocol, self._TEST_PAYLOAD_V1_BYTES)
self.assertEqual(self._TEST_PAYLOAD_V1_BYTES, MiConnectData.from_nfc_payload(payload).to_bytes())
self.assertEqual(self._TEST_PAYLOAD_V1.encode(), payload.appData.encode())
self.assertEqual(len(self._TEST_PAYLOAD_V1.encode()), payload.appData.size())

def test_v2_protocol(self) -> None:
payload = self._test_protocol(nfc.V2NfcProtocol, self._TEST_PAYLOAD_V2_BYTES)
self.assertEqual(self._TEST_PAYLOAD_V2_BYTES, MiConnectData.from_nfc_payload(payload).to_bytes())
self.assertEqual(self._TEST_PAYLOAD_V2.encode(), payload.appData.encode())
self.assertEqual(len(self._TEST_PAYLOAD_V2.encode()), payload.appData.size())

def test_handoff_protocol(self) -> None:
payload = self._test_protocol(nfc.HandoffNfcProtocol, self._TEST_PAYLOAD_HANDOFF_BYTES)
self.assertEqual(self._TEST_PAYLOAD_HANDOFF_BYTES, MiConnectData.from_nfc_payload(payload).to_bytes())
self.assertEqual(self._TEST_PAYLOAD_HANDOFF.encode(), payload.appData.encode())
self.assertEqual(len(self._TEST_PAYLOAD_HANDOFF.encode()), payload.appData.size())


if __name__ == "__main__":
Expand Down

0 comments on commit ce93bd5

Please sign in to comment.