Added unit tests, created functions to check the availability of updates.

This commit is contained in:
Jérémi N ‘EndMove’ 2022-09-11 13:38:36 +02:00
parent 6b26b89c69
commit 981a11ce83
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
11 changed files with 533 additions and 35 deletions

404
.gitignore vendored
View File

@ -1,6 +1,3 @@
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
@ -164,4 +161,403 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# End of https://www.toptal.com/developers/gitignore/api/python
### VisualStudio ###
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.tlog
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
*.vbp
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
*.dsw
*.dsp
# Visual Studio 6 technical files
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# Visual Studio History (VSHistory) files
.vshistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd
# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
# Local History for Visual Studio Code
.history/
# Windows Installer files from build outputs
*.cab
*.msi
*.msix
*.msm
*.msp
# JetBrains Rider
*.sln.iml
### VisualStudio Patch ###
# Additional files built by Visual Studio
# End of https://www.toptal.com/developers/gitignore/api/python,visualstudio

View File

@ -1,4 +1,4 @@
Version: 1.0.0.1
Version: 1.0.0
CompanyName: EndMove Corp.
FileDescription: Scraping tool to recover all the images of a website.
InternalName: webpic

View File

@ -1,6 +1,11 @@
from auto_py_to_exe import __main__ as apte
import pyinstaller_versionfile
"""
This file allows you to start auto-py in order to quickly and easily build the solution
into a viable .exe. Moreover this script will compile the meta data for windows.
"""
if __name__ == '__main__':
pyinstaller_versionfile.create_versionfile_from_input_file(
output_file="app_version_file.txt",

View File

@ -30,7 +30,7 @@ def get_config() -> dict:
'app_folder': os.getcwd(),
'app_version': '1.0.0', # This version must match with the version.txt at root
'app_version_date': '2022-09-05',
'app_version_source': 'https://git.endmove.eu/EndMove/WebPicDownloader/src/branch/dev/version.txt',
'app_version_source': 'https://git.endmove.eu/EndMove/WebPicDownloader/raw/branch/dev/VERSION',
'sys_version_matcher': re.compile(r'^(\d{1,2}\.)(\d{1,2}\.)(\d{1,2})$'),
'sys_directory': get_sys_directory(),

View File

@ -1,4 +1,11 @@
import unittest
"""
This file allows you to launch all the unit tests of the project.
Be careful these tests require internet.
"""
loader = unittest.TestLoader()
runner = unittest.TextTestRunner()
runner.run(loader.discover('./test/'))
runner.run(loader.discover('./tests/'))

View File

@ -1,11 +0,0 @@
import unittest
class TestUpdateModule(unittest.TestCase):
def test_lol(self):
self.assertTrue(True)
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,43 @@
import unittest
from re import fullmatch, compile, Pattern
class TestUpdateRegex(unittest.TestCase):
"""
Test Class for Update Regex
Versions can vary between 0.0.0 and 99.99.99 and cannot exceed two units.
0.0.0 to 0.99.99 = BETA
1.0.0 to 99.99.99 = RELEASE
"""
regex: Pattern = compile(r'^(\d{1,2}\.)(\d{1,2}\.)(\d{1,2})$')
def test_first_version(self):
self.assertTrue(fullmatch(self.regex, '1.0.0'))
def test_patch_version(self):
self.assertTrue(fullmatch(self.regex, '1.0.00'))
self.assertTrue(fullmatch(self.regex, '1.0.1'))
self.assertTrue(fullmatch(self.regex, '1.0.12'))
self.assertTrue(fullmatch(self.regex, '1.0.54'))
def test_minor_version(self):
self.assertTrue(fullmatch(self.regex, '1.00.0'))
self.assertTrue(fullmatch(self.regex, '1.1.0'))
self.assertTrue(fullmatch(self.regex, '1.98.0'))
self.assertTrue(fullmatch(self.regex, '1.24.0'))
def test_major_version(self):
self.assertTrue(fullmatch(self.regex, '00.0.0'))
self.assertTrue(fullmatch(self.regex, '10.0.0'))
self.assertTrue(fullmatch(self.regex, '99.0.0'))
self.assertTrue(fullmatch(self.regex, '42.0.0'))
def test_bad_version(self):
self.assertFalse(fullmatch(self.regex, '1.0.0000'))
self.assertFalse(fullmatch(self.regex, '1.00000.0'))
self.assertFalse(fullmatch(self.regex, '100.0.0'))
self.assertFalse(fullmatch(self.regex, '152.124.15'))
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,42 @@
import unittest
from urllib.error import HTTPError, URLError
from test_update_regex import TestUpdateRegex
from webpicdownloader.util.UpdateUtils import fetch_version, is_new_version_available
class TestUpdateUtils(unittest.TestCase):
"""
Test Class for Update Utils
<!> can not be executed directly <!>
* regex -> use UpdateRegex unittest regex.
* good_url -> use a permalink VERSION file.
* bad_url -> bad link that point a 404 error.
* bad_version -> use a permalink VERSION file for wich version format is bad.
"""
regex = TestUpdateRegex.regex
good_url = 'https://git.endmove.eu/EndMove/WebPicDownloader/raw/commit/6b26b89c6901841faaa09154c185d202223492c2/VERSION'
bad_url = 'https://git.endmove.eu/EndMove/WebPicDownloader/raw/commit/bad-commit/VERSION'
bad_version = 'https://git.endmove.eu/EndMove/WebPicDownloader/src/commit/6b26b89c6901841faaa09154c185d202223492c2/app_version_file.txt'
def test_fetch_version__good_url(self):
self.assertTrue(fetch_version(self.good_url) == '1.0.0')
def test_fetch_version__bad_url(self):
self.assertRaises(HTTPError, lambda: fetch_version(self.bad_url))
self.assertRaises(URLError, lambda: fetch_version('https://bad'))
self.assertRaises(ValueError, lambda: fetch_version('bad'))
def test_is_new_version_available__new_version_available(self):
self.assertTrue(is_new_version_available('0.0.0', self.good_url, self.regex))
self.assertTrue(is_new_version_available('0.48.0', self.good_url, self.regex))
self.assertTrue(is_new_version_available('0.48.14', self.good_url, self.regex))
self.assertTrue(is_new_version_available('5.48.14', self.good_url, self.regex))
def test_is_new_version_available__no_new_version_available(self):
self.assertFalse(is_new_version_available('1.0.0', self.good_url, self.regex))
def test_is_new_version_available__bad_version(self):
self.assertRaises(ValueError, lambda: is_new_version_available('1.0.0', self.bad_version, self.regex))
def test_is_new_version_available__bad_url(self):
self.assertRaises(HTTPError, lambda: is_new_version_available('1.0.0', self.bad_url, self.regex))

View File

@ -1,17 +0,0 @@
from http.client import HTTPException
import re
from urllib import request as http
def fetch_version(headers: str, url: str) -> str:
"""
"""
request = http.Request(url=url, headers=headers, method='GET')
response = http.urlopen(request)
if response.getcode() != 200:
raise HTTPException("Bad response returned by server")
return response.read().decode('utf-8')

View File

@ -0,0 +1,33 @@
from re import fullmatch, Pattern
from urllib import request as http
from urllib.error import HTTPError
def fetch_version(url: str) -> str:
"""
Retrieve the latest webpicdownloader version available.
* :url: -> Url of the "VERSION" file on the repository.
* RETURN -> the latests 'VERSION' file content.
* THROWABLE -> can raise HTTP or URL error see urllib doc.
"""
headers = {'User-Agent': "Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/605.1.15 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/604.1"}
request = http.Request(url=url, headers=headers, method='GET')
response = http.urlopen(request)
if response.getcode() != 200:
raise HTTPError("Bad response returned by server")
return response.read().decode('utf-8')
def is_new_version_available(version_current: str, version_url: str, version_pattern: Pattern) -> bool:
"""
Verify if a new version is available.
* :current: -> Current version
* :version_url: -> Url of the VERSION file on the repository.
* :version_pattern: -> Patter to match version retrieved with the version_url.
* RETURN -> True means a new version is available, False else.
"""
version = fetch_version(version_url)
if not fullmatch(version_pattern, version):
raise ValueError('The version retrieved from the remote server is not valid')
return version_current != version