FreeCAD
138 строк · 5.9 Кб
1# SPDX-License-Identifier: LGPL-2.1-or-later
2# ***************************************************************************
3# * *
4# * Copyright (c) 2022-2023 FreeCAD Project Association *
5# * *
6# * This file is part of FreeCAD. *
7# * *
8# * FreeCAD is free software: you can redistribute it and/or modify it *
9# * under the terms of the GNU Lesser General Public License as *
10# * published by the Free Software Foundation, either version 2.1 of the *
11# * License, or (at your option) any later version. *
12# * *
13# * FreeCAD is distributed in the hope that it will be useful, but *
14# * WITHOUT ANY WARRANTY; without even the implied warranty of *
15# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16# * Lesser General Public License for more details. *
17# * *
18# * You should have received a copy of the GNU Lesser General Public *
19# * License along with FreeCAD. If not, see *
20# * <https://www.gnu.org/licenses/>. *
21# * *
22# ***************************************************************************
23
24import unittest25import os26import FreeCAD27
28from Addon import Addon29
30from addonmanager_utilities import (31recognized_git_location,32get_readme_url,33get_assigned_string_literal,34get_macro_version_from_file,35)
36
37
38class TestUtilities(unittest.TestCase):39
40MODULE = "test_utilities" # file name without extension41
42def setUp(self):43self.test_dir = os.path.join(44FreeCAD.getHomePath(), "Mod", "AddonManager", "AddonManagerTest", "data"45)46
47@classmethod48def tearDownClass(cls):49try:50os.remove("AM_INSTALLATION_DIGEST.txt")51except FileNotFoundError:52pass53
54def test_recognized_git_location(self):55recognized_urls = [56"https://github.com/FreeCAD/FreeCAD",57"https://gitlab.com/freecad/FreeCAD",58"https://framagit.org/freecad/FreeCAD",59"https://salsa.debian.org/science-team/freecad",60]61for url in recognized_urls:62repo = Addon("Test Repo", url, Addon.Status.NOT_INSTALLED, "branch")63self.assertTrue(recognized_git_location(repo), f"{url} was unexpectedly not recognized")64
65unrecognized_urls = [66"https://google.com",67"https://freecad.org",68"https://not.quite.github.com/FreeCAD/FreeCAD",69"https://github.com.malware.com/",70]71for url in unrecognized_urls:72repo = Addon("Test Repo", url, Addon.Status.NOT_INSTALLED, "branch")73self.assertFalse(recognized_git_location(repo), f"{url} was unexpectedly recognized")74
75def test_get_readme_url(self):76github_urls = [77"https://github.com/FreeCAD/FreeCAD",78]79gitlab_urls = [80"https://gitlab.com/freecad/FreeCAD",81"https://framagit.org/freecad/FreeCAD",82"https://salsa.debian.org/science-team/freecad",83"https://unknown.location/and/path",84]85
86# GitHub and Gitlab have two different schemes for file URLs: unrecognized URLs are87# presumed to be local instances of a GitLab server. Note that in neither case does this88# take into account the redirects that are used to actually fetch the data.89
90for url in github_urls:91branch = "branchname"92expected_result = f"{url}/raw/{branch}/README.md"93repo = Addon("Test Repo", url, Addon.Status.NOT_INSTALLED, branch)94actual_result = get_readme_url(repo)95self.assertEqual(actual_result, expected_result)96
97for url in gitlab_urls:98branch = "branchname"99expected_result = f"{url}/-/raw/{branch}/README.md"100repo = Addon("Test Repo", url, Addon.Status.NOT_INSTALLED, branch)101actual_result = get_readme_url(repo)102self.assertEqual(actual_result, expected_result)103
104def test_get_assigned_string_literal(self):105good_lines = [106["my_var = 'Single-quoted literal'", "Single-quoted literal"],107['my_var = "Double-quoted literal"', "Double-quoted literal"],108["my_var = \t 'Extra whitespace'", "Extra whitespace"],109["my_var = 42", "42"],110["my_var = 1.23", "1.23"],111]112for line in good_lines:113result = get_assigned_string_literal(line[0])114self.assertEqual(result, line[1])115
116bad_lines = [117"my_var = __date__",118"my_var 'No equals sign'",119"my_var = 'Unmatched quotes\"",120"my_var = No quotes at all",121"my_var = 1.2.3",122]123for line in bad_lines:124result = get_assigned_string_literal(line)125self.assertIsNone(result)126
127def test_get_macro_version_from_file(self):128good_file = os.path.join(self.test_dir, "good_macro_metadata.FCStd")129version = get_macro_version_from_file(good_file)130self.assertEqual(version, "1.2.3")131
132bad_file = os.path.join(self.test_dir, "bad_macro_metadata.FCStd")133version = get_macro_version_from_file(bad_file)134self.assertEqual(version, "", "Bad version did not yield empty string")135
136empty_file = os.path.join(self.test_dir, "missing_macro_metadata.FCStd")137version = get_macro_version_from_file(empty_file)138self.assertEqual(version, "", "Missing version did not yield empty string")139