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

Add ability to create json files from issues #12

Merged
merged 10 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
10 changes: 9 additions & 1 deletion .github/workflows/testCode.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,22 @@ jobs:
python -m pip install --upgrade pip
python -m pip install tox
- name: Test with tox
# Run automated/unit tests
run: tox
- name: Lint with flake8
# Check code with the linter
run: runlint
shell: cmd
- name: Validate metadata
# E2E: test to check the script can be run, no need to actually test the file.
# The internal checks are covered with unit tests.
run: runvalidate --dry-run _test/testData/addons/fake/13.0.json
shell: cmd
- name: Get sha256
# E2E: test to check the script can be run
run: runsha _tests\testData\fake.nvda-addon
shell: cmd

- name: Generate json file
# E2E: test to check the script can be run
run: runcreatejson -f _tests\testData\fake.nvda-addon --dir _tests\testOutput/test_runcreatejson --channel=stable --publisher=fakepublisher --sourceUrl=https://github.com/fake/ --url=https://github.com/fake.nvda-addon --licName="GPL v2" --licUrl="https://www.gnu.org/licenses/gpl-2.0.html"
shell: cmd
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.tox
.venv
__pycache__
_tests/testOutput
14 changes: 7 additions & 7 deletions _tests/testData/addons/fake/13.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"minor": 0,
"patch": 0
},
"displayName": "Clip Contents Designer",
"description": "Add-on for managing clipboard text.",
"homepage": "https://github.com/nvdaes/clipContentsDesigner",
"publisher": "nvdaes",
"displayName": "mock addon",
"description": "The description for the addon",
"homepage": "https://nvaccess.org",
"publisher": "Name of addon author or organisation",
"minNVDAVersion": {
"major": 2019,
"minor": 3,
Expand All @@ -21,10 +21,10 @@
"patch": 0
},
"channel": "stable",
"URL": "https://github.com/nvdaes/clipContentsDesigner/releases/download/13.0/clipContentsDesigner-13.0.nvda-addon",
"URL": "https://github.com/nvaccess/dont/use/this/address/fake.nvda-addon",
"sha256-comment": "SHA for the fake.nvda-addon file",
"sha256": "12ABBF6BAC89FC24602245F4B8B750BCF2B72AFDF2DF54A0B07467FF4983F872",
"sourceURL": "https://github.com/nvdaes/clipContentsDesigner/",
"sha256": "755b4a427551c8fb6a8ff46d77752bc8198d4553e5a60b488520b1c77db2fd11",
"sourceURL": "https://github.com/nvaccess/dont/use/this/address",
"license": "GPL v2",
"licenseURL": "https://www.gnu.org/licenses/gpl-2.0.html"
}
Binary file modified _tests/testData/fake.nvda-addon
Binary file not shown.
8 changes: 4 additions & 4 deletions _tests/testData/manifest.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name = fake
summary = "Clip Contents Designer"
description = """Add-on for managing clipboard text."""
author = "Noelia <[email protected]>, Abdel <[email protected]"
url = https://github.com/nvdaes/clipContentsDesigner
summary = "mock addon"
description = """The description for the addon"""
author = "Name of addon author or organisation"
url = https://nvaccess.org
version = 13.0
docFileName = readme.html
minimumNVDAVersion = 2019.3
Expand Down
141 changes: 141 additions & 0 deletions _tests/test_createJson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/usr/bin/env python

# Copyright (C) 2022 Noelia Ruiz Martínez, NV Access Limited
# This file may be used under the terms of the GNU General Public License, version 2 or later.
# For more details see: https://www.gnu.org/licenses/gpl-2.0.html

import unittest
import os
import shutil
import json
from _validate import (
createJson,
addonManifest
)

TOP_DIR = os.path.abspath(os.path.dirname(__file__))
SOURCE_DIR = os.path.dirname(TOP_DIR)
INPUT_DATA_PATH = os.path.join(SOURCE_DIR, '_tests', 'testData')
VALID_JSON = os.path.join(
INPUT_DATA_PATH,
"addons",
"fake",
"13.0.0.json"
) # json file available in testData/fake
ADDON_PACKAGE = os.path.join(INPUT_DATA_PATH, 'fake.nvda-addon')
MANIFEST_FILE = os.path.join(INPUT_DATA_PATH, 'manifest.ini')
ADDON_CHANNEL = "testChannel"
ADDON_PUBLISHER = "testPublisher"
ADDON_SOURCE_URL = "https://example.com/"

OUTPUT_DATA_PATH = os.path.join(SOURCE_DIR, '_tests', 'testOutput')


def getAddonManifest():
feerrenrut marked this conversation as resolved.
Show resolved Hide resolved
with open(MANIFEST_FILE) as f:
manifest = addonManifest.AddonManifest(f)
return manifest


class IntegrationTestCreateJson(unittest.TestCase):
""" Integration tests.
- The JSON file is created (written to the filesystem).
- The output is then loaded and checked for correctness.
"""
def setUp(self):
self.outputDir = os.path.join(OUTPUT_DATA_PATH, "createJsonOutput")
self.maxDiff = None
feerrenrut marked this conversation as resolved.
Show resolved Hide resolved
if os.path.isdir(self.outputDir):
shutil.rmtree(self.outputDir)

def test_contentsMatchesExampleFile(self):
# Values used must match the manifest files:
# - '_tests / testData / manifest.ini'
# - '_tests/testData/fake.nvda-addon' (unzip)
createJson.generateJsonFile(
ADDON_PACKAGE,
self.outputDir,
channel="stable",
publisher="Name of addon author or organisation",
sourceUrl="https://github.com/nvaccess/dont/use/this/address",
url="https://github.com/nvaccess/dont/use/this/address/fake.nvda-addon",
licenseName="GPL v2",
licenseUrl="https://www.gnu.org/licenses/gpl-2.0.html",
)
actualJsonPath = os.path.join(self.outputDir, "fake", "13.0.0.json")
self.assertTrue(
os.path.isfile(actualJsonPath),
f"Failed to create json file: {actualJsonPath}"
)
self._assertJsonFilesEqual(actualJsonPath=actualJsonPath, expectedJsonPath=VALID_JSON)

def _assertJsonFilesEqual(self, actualJsonPath: str, expectedJsonPath: str):

# Not equal, how are they different?
with open(VALID_JSON) as expectedFile:
expectedJson = json.load(expectedFile)
del expectedJson["sha256-comment"] # remove explanatory comment
with open(actualJsonPath) as actualFile:
actualJson = json.load(actualFile)

self.assertDictEqual(actualJson, expectedJson)


class Test_getVersionNumber(unittest.TestCase):
def test_tripleDigitVersion_isValid(self):
""" Canonical version (major, minor, patch) expected.
"""
versionNumber = createJson.getVersionNumber("1.2.3")
self.assertEqual(versionNumber.major, 1)
self.assertEqual(versionNumber.minor, 2)
self.assertEqual(versionNumber.patch, 3)

def test_doubleDigitVersion_isValid(self):
"""patch is optional, assumed to be zero.
"""
versionNumber = createJson.getVersionNumber("1.02")
self.assertEqual(versionNumber.major, 1)
self.assertEqual(versionNumber.minor, 2)
self.assertEqual(versionNumber.patch, 0)

def test_singleDigitVersion_raises(self):
with self.assertRaises(ValueError, msg="Single digit version numbers are expected to be an error."):
createJson.getVersionNumber("1")

def test_tooManyValues_raises(self):
with self.assertRaises(ValueError, msg="More than three parts is expected as an error."):
createJson.getVersionNumber("1.2.3.4")

def test_versionWithNonDigit(self):
with self.assertRaises(
ValueError,
msg="Non-digit chars in version number expected as an error."):
createJson.getVersionNumber("1.2.3a")


class Test_buildOutputFilePath(unittest.TestCase):
def setUp(self) -> None:
self.outputDir = os.path.join(OUTPUT_DATA_PATH, "test_buildOutputFilePath")
if os.path.isdir(self.outputDir):
shutil.rmtree(self.outputDir)

def test_validVersion(self):
outputFilePath = createJson.buildOutputFilePath(
data={
"addonId": "testId",
"addonVersionNumber": {
"major": 1,
"minor": 2,
"patch": 0,
}
},
parentDir=self.outputDir
)

dir, filename = os.path.split(outputFilePath)
self.assertTrue(os.path.isdir(dir), msg="Directory must exist.")
self.assertEqual(
filename,
"1.2.0.json",
msg="Name of the output file should be named based on version number"
)
5 changes: 2 additions & 3 deletions _tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_missingHTTPsAndExt(self):
class Validate_checkSha256(unittest.TestCase):
"""Tests for the checkSha256 function
"""
validSha = "12ABBF6BAC89FC24602245F4B8B750BCF2B72AFDF2DF54A0B07467FF4983F872"
validSha = "755B4A427551C8FB6A8FF46D77752BC8198D4553E5A60B488520B1C77DB2FD11"

def test_valid(self):
errors = validate.checkSha256(
Expand Down Expand Up @@ -560,8 +560,7 @@ def test_downloadFailure(self, mock_urlopen):
'Fatal error, unable to continue: Unable to download from '
# note this the mocked urlopen function actually fetches from ADDON_PACKAGE
'https://github.com/'
'nvdaes/clipContentsDesigner/releases/download/13.0/'
'clipContentsDesigner-13.0.nvda-addon, '
'nvaccess/dont/use/this/address/fake.nvda-addon, '
'HTTP response status code: 404'
]
)
Expand Down
Loading