Coverage for gpkit\tests\test_repo.py: 0%
73 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 16:57 -0500
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 16:57 -0500
1"Implements tests for all external repositories."
2import os
3import sys
4import subprocess
5from time import sleep
6from collections import defaultdict
9def test_repo(repo=".", xmloutput=False):
10 """Test repository.
12 If no repo name given, runs in current directory.
13 Otherwise, assumes is in directory above the repo
14 with a shared gplibrary repository.
15 """
16 os.chdir(repo)
17 settings = get_settings()
18 print("")
19 print("SETTINGS")
20 print(settings)
21 print("")
23 if repo == "." and not os.path.isdir("gpkitmodels"):
24 git_clone("gplibrary")
25 pip_install("gplibrary", local=True)
27 # install dependencies other than gplibrary
28 if settings["pip install"]:
29 for package in settings["pip install"].split(","):
30 package = package.strip()
31 pip_install(package)
32 if os.path.isfile("setup.py"):
33 pip_install(".")
35 skipsolvers = None
36 if "skipsolvers" in settings:
37 skipsolvers = [s.strip() for s in settings["skipsolvers"].split(",")]
39 testpy = ("from gpkit.tests.from_paths import run;"
40 "run(xmloutput=%s, skipsolvers=%s)"
41 % (xmloutput, skipsolvers))
42 subprocess.call(["python", "-c", testpy])
43 if repo != ".":
44 os.chdir("..")
47def test_repos(repos=None, xmloutput=False, ingpkitmodels=False):
48 """Get the list of external repos to test, and test.
50 Arguments
51 ---------
52 xmloutput : bool
53 True if the tests should produce xml reports
55 ingpkitmodels : bool
56 False if you're in the gpkitmodels directory that should be considered
57 as the default. (overriden by repo-specific branch specifications)
58 """
59 if not ingpkitmodels:
60 git_clone("gplibrary")
61 repos_list_filename = "gplibrary"+os.sep+"EXTERNALTESTS"
62 pip_install("gplibrary", local=True)
63 else:
64 print("USING LOCAL DIRECTORY AS GPKITMODELS DIRECTORY")
65 repos_list_filename = "EXTERNALTESTS"
66 pip_install(".", local=True)
67 repos = [line.strip() for line in open(repos_list_filename, "r")]
68 for repo in repos:
69 git_clone(repo)
70 test_repo(repo, xmloutput)
73def get_settings():
74 "Gets settings from a TESTCONFIG file"
75 settings = defaultdict(str)
76 if os.path.isfile("TESTCONFIG"):
77 with open("TESTCONFIG", "r") as f:
78 for line in f:
79 if len(line.strip().split(" : ")) > 1:
80 key, value = line.strip().split(" : ")
81 settings[key] = value
82 return settings
85def git_clone(repo, branch="master"):
86 "Tries several times to clone a given repository"
87 cmd = ["git", "clone", "--depth", "1"]
88 cmd += ["-b", branch]
89 cmd += ["https://github.com/convexengineering/%s.git" % repo]
90 call_and_retry(cmd)
93def pip_install(package, local=False):
94 "Tries several times to install a pip package"
95 if sys.platform == "win32":
96 cmd = ["pip"]
97 else:
98 cmd = ["python", os.environ["PIP"]]
99 cmd += ["install"]
100 if local:
101 cmd += ["--no-cache-dir", "--no-deps", "-e"]
102 cmd += [package]
103 call_and_retry(cmd)
106def call_and_retry(cmd, max_iterations=5, delay=5):
107 "Tries max_iterations times (waiting d each time) to run a command"
108 iterations = 0
109 return_code = None
110 print("calling", cmd)
111 while return_code != 0 and iterations < max_iterations:
112 iterations += 1
113 print(" attempt", iterations)
114 return_code = subprocess.call(cmd)
115 sleep(delay)
116 return return_code