FreeCAD

Форк
0
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

24
import unittest
25
import os
26
import FreeCAD
27

28
from Addon import Addon
29

30
from addonmanager_utilities import (
31
    recognized_git_location,
32
    get_readme_url,
33
    get_assigned_string_literal,
34
    get_macro_version_from_file,
35
)
36

37

38
class TestUtilities(unittest.TestCase):
39

40
    MODULE = "test_utilities"  # file name without extension
41

42
    def setUp(self):
43
        self.test_dir = os.path.join(
44
            FreeCAD.getHomePath(), "Mod", "AddonManager", "AddonManagerTest", "data"
45
        )
46

47
    @classmethod
48
    def tearDownClass(cls):
49
        try:
50
            os.remove("AM_INSTALLATION_DIGEST.txt")
51
        except FileNotFoundError:
52
            pass
53

54
    def test_recognized_git_location(self):
55
        recognized_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
        ]
61
        for url in recognized_urls:
62
            repo = Addon("Test Repo", url, Addon.Status.NOT_INSTALLED, "branch")
63
            self.assertTrue(recognized_git_location(repo), f"{url} was unexpectedly not recognized")
64

65
        unrecognized_urls = [
66
            "https://google.com",
67
            "https://freecad.org",
68
            "https://not.quite.github.com/FreeCAD/FreeCAD",
69
            "https://github.com.malware.com/",
70
        ]
71
        for url in unrecognized_urls:
72
            repo = Addon("Test Repo", url, Addon.Status.NOT_INSTALLED, "branch")
73
            self.assertFalse(recognized_git_location(repo), f"{url} was unexpectedly recognized")
74

75
    def test_get_readme_url(self):
76
        github_urls = [
77
            "https://github.com/FreeCAD/FreeCAD",
78
        ]
79
        gitlab_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 are
87
        # presumed to be local instances of a GitLab server. Note that in neither case does this
88
        # take into account the redirects that are used to actually fetch the data.
89

90
        for url in github_urls:
91
            branch = "branchname"
92
            expected_result = f"{url}/raw/{branch}/README.md"
93
            repo = Addon("Test Repo", url, Addon.Status.NOT_INSTALLED, branch)
94
            actual_result = get_readme_url(repo)
95
            self.assertEqual(actual_result, expected_result)
96

97
        for url in gitlab_urls:
98
            branch = "branchname"
99
            expected_result = f"{url}/-/raw/{branch}/README.md"
100
            repo = Addon("Test Repo", url, Addon.Status.NOT_INSTALLED, branch)
101
            actual_result = get_readme_url(repo)
102
            self.assertEqual(actual_result, expected_result)
103

104
    def test_get_assigned_string_literal(self):
105
        good_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
        ]
112
        for line in good_lines:
113
            result = get_assigned_string_literal(line[0])
114
            self.assertEqual(result, line[1])
115

116
        bad_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
        ]
123
        for line in bad_lines:
124
            result = get_assigned_string_literal(line)
125
            self.assertIsNone(result)
126

127
    def test_get_macro_version_from_file(self):
128
        good_file = os.path.join(self.test_dir, "good_macro_metadata.FCStd")
129
        version = get_macro_version_from_file(good_file)
130
        self.assertEqual(version, "1.2.3")
131

132
        bad_file = os.path.join(self.test_dir, "bad_macro_metadata.FCStd")
133
        version = get_macro_version_from_file(bad_file)
134
        self.assertEqual(version, "", "Bad version did not yield empty string")
135

136
        empty_file = os.path.join(self.test_dir, "missing_macro_metadata.FCStd")
137
        version = get_macro_version_from_file(empty_file)
138
        self.assertEqual(version, "", "Missing version did not yield empty string")
139

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.