diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3ef28f9..560ded1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e6fa664..c5774fc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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/ diff --git a/build_protobuf.py b/build_protobuf.py index f5ca8db..25a6016 100644 --- a/build_protobuf.py +++ b/build_protobuf.py @@ -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") @@ -12,11 +12,16 @@ 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([ @@ -24,5 +29,5 @@ f"--proto_path=\"{PROTO_DIR}\"", f"--pyi_out=\"{PYTHON_OUTPUT_DIR}\"", f"--python_out=\"{PYTHON_OUTPUT_DIR}\"", - f"\"{PROTO_DIR}/*\"" + f"\"{PROTO_DIR.joinpath('*')}\"" ])) diff --git a/src/xiaomi_ndef/nfc.py b/src/xiaomi_ndef/nfc.py index 87b5578..a3ef019 100644 --- a/src/xiaomi_ndef/nfc.py +++ b/src/xiaomi_ndef/nfc.py @@ -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" @@ -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" @@ -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" diff --git a/tests/test_xiaomi_ndef.py b/tests/test_xiaomi_ndef.py index 00e4a7a..bbd19ea 100644 --- a/tests/test_xiaomi_ndef.py +++ b/tests/test_xiaomi_ndef.py @@ -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__":