def __init__(self, path: str, replacement: str = "") -> None: # First replace the normalized path, then the Unix-style path (which # some tool may output even on Windows) and finally the very path that # was given. super().__init__([ Substitute(substring, replacement) for substring in [os.path.realpath(path), unixpath(path), path] ])
def file_url(path, unix=False): if sys.platform == 'win32': if len(path) > 1 and path[1] == ':': # svn info returns the URL with an uppercase letter drive path = path[0].upper() + path[1:] if unix: return 'file://' + unixpath(path) else: return 'file:///' + path.replace('\\', '/') else: return 'file://' + path
def file_url(path, unix=False): if sys.platform == "win32": if len(path) > 1 and path[1] == ":": # svn info returns the URL with an uppercase letter drive path = path[0].upper() + path[1:] if unix: return "file://" + unixpath(path) else: return "file:///" + path.replace("\\", "/") else: return "file://" + path
def encode(cls, path): """Encode a test name. Use the dirname to compute the testcase display name. This uses mappings set in the configuration file. This file is loaded if necessary. """ cls._load_config() path, basename = os.path.split(unixpath(path)) return '%s.%s' % (cls.encoding['/'.join(path.split('/'))], basename)
def cmdline( self, target: Optional[Union[str, List[str]]] = None, jobs: Optional[int] = None, exec_dir: Optional[str] = None, timeout: Optional[float] = None, ) -> Dict[str, Union[List[str], dict]]: """Return the make command line. :param target: optional target or list of targets to use (use default_target if None) :param jobs: see __init__ documentation :param exec_dir: see __init__ documentation :param timeout: timeout to pass to ex.Run :return: a dictionary with the following keys - cmd: containing the command line to pass to gnatpython.ex.Run - options: options to pass to gnatpython.ex.Run """ cmd_arg_list = ["make"] if self.makefile is not None: cmd_arg_list += ["-f", unixpath(self.makefile)] cmd_arg_list += [ "-j", "%s" % str(jobs) if jobs is not None else str(self.jobs) ] for key in self.var_list: cmd_arg_list.append("{}={}".format(key, self.var_list[key])) if target is None: target = self.default_target if target is not None: if isinstance(target, list): cmd_arg_list += target else: cmd_arg_list.append(target) options = {"cwd": exec_dir or self.exec_dir, "timeout": timeout} return { "cmd": parse_command(command=cmd_arg_list, build_space=self.anod_instance.build_space), "options": options, }
def cmdline(self): """Return the configure command line. :return: a dictionary with the following keys - cmd: containing the command line to pass to gnatpython.ex.Run - options: options to pass to gnatpython.ex.Run :rtype: dict If CONFIG_SHELL environment variable is set, the configure will be called with this shell. """ cmd = [] if "CONFIG_SHELL" in os.environ: cmd.append(os.environ["CONFIG_SHELL"]) # Compute the relative path for configure configure_path = unixpath( os.path.relpath(os.path.join(self.src_dir, "configure"), self.exec_dir)) # In case the configure is run from its location ensure to # add ./ as . is not necessary in PATH. if configure_path == "configure": configure_path = "./configure" cmd += [configure_path] cmd += self.args if self.target is not None: cmd.append("--target=" + self.target) if self.host is not None: cmd.append("--host=" + self.host) if self.build is not None: cmd.append("--build=" + self.build) cmd_options = { "cwd": self.exec_dir, "ignore_environ": False, "env": self.env } return { "cmd": parse_command(command=cmd, build_space=self.anod_instance.build_space), "options": cmd_options, }
def cmdline(self, target=None, jobs=None, exec_dir=None, timeout=None): """Return the make command line. :param target: the target to use (use default_target if None) :type target: str | None :param jobs: see __init__ documentation :type jobs: int | None :param exec_dir: see __init__ documentation :type exec_dir: str | None :param timeout: timeout to pass to ex.Run :type timeout: int | None :return: a dictionary with the following keys - cmd: containing the command line to pass to gnatpython.ex.Run - options: options to pass to gnatpython.ex.Run :rtype: dict """ cmd_arg_list = ["make"] if self.makefile is not None: cmd_arg_list += ["-f", unixpath(self.makefile)] cmd_arg_list += [ "-j", "%s" % str(jobs) if jobs is not None else str(self.jobs) ] for key in self.var_list: cmd_arg_list.append("%s=%s" % (key, self.var_list[key])) if target is None: target = self.default_target if target is not None: if isinstance(target, list): cmd_arg_list += target else: cmd_arg_list.append(target) options = {"cwd": exec_dir or self.exec_dir, "timeout": timeout} return { "cmd": parse_command(command=cmd_arg_list, build_space=self.anod_instance.build_space), "options": options, }
def cmdline(self, target=None, jobs=None, exec_dir=None, timeout=None): """Return the make command line. :param target: the target to use (use default_target if None) :type target: str | None :param jobs: see __init__ documentation :type jobs: int | None :param exec_dir: see __init__ documentation :type exec_dir: str | None :param timeout: timeout to pass to ex.Run :type timeout: int | None :return: a dictionary with the following keys - cmd: containing the command line to pass to gnatpython.ex.Run - options: options to pass to gnatpython.ex.Run :rtype: dict """ cmd_arg_list = ['make'] if self.makefile is not None: cmd_arg_list += ['-f', unixpath(self.makefile)] cmd_arg_list += [ '-j', '%s' % str(jobs) if jobs is not None else str(self.jobs)] for key in self.var_list: cmd_arg_list.append("%s=%s" % (key, self.var_list[key])) if target is None: target = self.default_target if target is not None: if isinstance(target, list): cmd_arg_list += target else: cmd_arg_list.append(target) options = { 'cwd': exec_dir or self.exec_dir, 'timeout': timeout} return { 'cmd': parse_command( command=cmd_arg_list, build_space=self.anod_instance.build_space), 'options': options}
def cmdline(self): """Return the configure command line. :return: a dictionary with the following keys - cmd: containing the command line to pass to gnatpython.ex.Run - options: options to pass to gnatpython.ex.Run :rtype: dict If CONFIG_SHELL environment variable is set, the configure will be called with this shell. """ cmd = [] if 'CONFIG_SHELL' in os.environ: cmd.append(os.environ['CONFIG_SHELL']) cmd += [ unixpath( os.path.relpath(os.path.join(self.src_dir, 'configure'), self.exec_dir)) ] cmd += self.args if self.target is not None: cmd.append('--target=' + self.target) if self.host is not None: cmd.append('--host=' + self.host) if self.build is not None: cmd.append('--build=' + self.build) cmd_options = { 'cwd': self.exec_dir, 'ignore_environ': False, 'env': self.env } return { 'cmd': parse_command(command=cmd, build_space=self.anod_instance.build_space), 'options': cmd_options }
def check_run(self, previous_values): """Check status fragment.""" sync_tree(self.test_env['test_dir'], self.test_env['working_dir']) process = check_call_valgrind(self, [self.env.gsh, './test.sh'], timeout=self.process_timeout) with open(os.path.join(self.test_env['test_dir'], 'test.out'), 'r') as fd: expected_output = fd.read() actual_output = process.out.replace(self.test_env['working_dir'], '') actual_output = actual_output.replace('\\', '/') actual_output = actual_output.replace( unixpath(self.test_env['working_dir']), '') d = diff(actual_output, expected_output) if not d: self.result.set_status(TestStatus.PASS) else: self.result.set_status(TestStatus.FAIL, d) self.push_result()
def cmdline(self): """Return the configure command line. :return: a dictionary with the following keys - cmd: containing the command line to pass to gnatpython.ex.Run - options: options to pass to gnatpython.ex.Run :rtype: dict If CONFIG_SHELL environment variable is set, the configure will be called with this shell. """ cmd = [] if 'CONFIG_SHELL' in os.environ: cmd.append(os.environ['CONFIG_SHELL']) cmd += [unixpath(os.path.relpath( os.path.join(self.src_dir, 'configure'), self.exec_dir))] cmd += self.args if self.target is not None: cmd.append('--target=' + self.target) if self.host is not None: cmd.append('--host=' + self.host) if self.build is not None: cmd.append('--build=' + self.build) cmd_options = {'cwd': self.exec_dir, 'ignore_environ': False, 'env': self.env} return { 'cmd': parse_command( command=cmd, build_space=self.anod_instance.build_space), 'options': cmd_options}
def test_git_repo(): working_tree = os.path.join(os.getcwd(), 'working_tree') working_tree2 = os.path.join(os.getcwd(), 'working_tree2') repo = GitRepository(working_tree) repo.init() os.chdir(working_tree) new_file = os.path.join(working_tree, 'new.txt') echo_to_file(new_file, 'new\n') commit_note = unixpath(os.path.join(working_tree, 'commit_note.txt')) echo_to_file( commit_note, '\n'.join( ('Code-Review+2: Nobody <*****@*****.**>', 'Submitted-by: Nobody <*****@*****.**>', 'Submitted-at: Thu, 08 Jun 2017 18:40:11 +0200'))) repo.git_cmd(['add', 'new.txt']) repo.git_cmd(['config', 'user.email', '*****@*****.**']) repo.git_cmd(['config', 'user.name', 'e3 core']) repo.git_cmd(['commit', '-m', 'new file']) repo.git_cmd(['tag', '-a', '-m', 'new tag', '20.0855369232']) repo.git_cmd( ['notes', '--ref', 'review', 'add', 'HEAD', '-F', commit_note]) # try with gerrit notes with open('log.txt', 'w') as f: repo.write_log(f, with_gerrit_notes=True) with open('log.txt', 'r') as f: commits = list(repo.parse_log(f)) assert '*****@*****.**' in commits[0]['notes']['Code-Review+2'] # try with an invalid note repo.git_cmd([ 'notes', '--ref', 'review', 'add', 'HEAD', '-f', '-m', 'invalid-note' ]) with open('log.txt', 'w') as f: repo.write_log(f, with_gerrit_notes=True) with open('log.txt', 'r') as f: commits = list(repo.parse_log(f)) assert commits[0]['notes'] is None # try again without gerrit notes with open('log.txt', 'w') as f: repo.write_log(f) with open('log.txt', 'r') as f: commits = list(repo.parse_log(f)) assert 'new file' in commits[0]['message'] assert commits[0]['email'] == '*****@*****.**' new_sha = commits[0]['sha'] assert '20.0855369232' in repo.describe() assert new_sha == repo.rev_parse() with pytest.raises(GitError) as err: repo.describe('g') assert 'describe --always g' in str(err) echo_to_file(new_file, 'new line\n', append=True) with open('commit1.diff', 'wb') as f: repo.write_local_diff(f) with open('commit1.diff', 'rb') as f: assert b'+new line' in f.read() echo_to_file(new_file, 10000 * '*') repo.git_cmd(['commit', '-a', '-m', 'file update']) with open('log2.txt', 'w') as f: repo.write_log(f) with open('log2.txt', 'r') as f: commits = list(repo.parse_log(f, max_diff_size=1000)) # assert b'diff too long' not in commits[1]['diff'] assert 'file update' in commits[0]['message'] assert 'diff too long' in commits[0]['diff'] assert 'new file' in commits[1]['message'] assert commits[1]['sha'] != commits[0]['sha'] assert commits[1]['diff'] != commits[0]['diff'] repo2 = GitRepository(working_tree2) giturl = 'file://%s' % working_tree.replace('\\', '/') repo2.init(url=giturl, remote='tree1') repo2.update(url=giturl, refspec='master') repo2.rev_parse() == repo.rev_parse() repo2.fetch_gerrit_notes(url=giturl) p = repo2.git_cmd(['notes', '--ref=review', 'show', new_sha], output=subprocess.PIPE) assert 'invalid-note' in p.out
def test_git_repo(): working_tree = unixpath(os.path.join(os.getcwd(), 'working_tree')) working_tree2 = os.path.join(os.getcwd(), 'working_tree2') repo = GitRepository(working_tree) repo.init() os.chdir(working_tree) new_file = unixpath(os.path.join(working_tree, 'new.txt')) echo_to_file(new_file, 'new\n') repo.git_cmd(['add', 'new.txt']) repo.git_cmd(['config', 'user.email', '*****@*****.**']) repo.git_cmd(['config', 'user.name', 'e3 core']) repo.git_cmd(['commit', '-m', 'new file']) repo.git_cmd(['tag', '-a', '-m', 'new tag', '20.0855369232']) with open('log.txt', 'w') as f: repo.write_log(f) with open('log.txt', 'r') as f: commits = list(repo.parse_log(f)) assert 'new file' in commits[0]['message'] assert commits[0]['email'] == '*****@*****.**' new_sha = commits[0]['sha'] assert '20.0855369232' in repo.describe() assert new_sha == repo.rev_parse() with pytest.raises(GitError) as err: repo.describe('g') assert 'describe --always g' in str(err) echo_to_file(new_file, 'new line\n', append=True) with open('commit1.diff', 'wb') as f: repo.write_local_diff(f) with open('commit1.diff', 'rb') as f: assert b'+new line' in f.read() echo_to_file(new_file, 10000 * '*') repo.git_cmd(['commit', '-a', '-m', 'file update']) with open('log2.txt', 'w') as f: repo.write_log(f) with open('log2.txt', 'r') as f: commits = list(repo.parse_log(f, max_diff_size=1000)) # assert b'diff too long' not in commits[1]['diff'] assert 'file update' in commits[0]['message'] assert 'diff too long' in commits[0]['diff'] assert 'new file' in commits[1]['message'] assert commits[1]['sha'] != commits[0]['sha'] assert commits[1]['diff'] != commits[0]['diff'] repo2 = GitRepository(working_tree2) repo2.init(url=working_tree, remote='tree1') try: repo2.update(url=working_tree, refspec='master') except GitError: if sys.platform == 'win32': # some git versions on windows do not support that well # ignore this test for now??? pass else: repo2.rev_parse() == repo.rev_parse()
def test_svn_repo(): cwd = os.getcwd() # --- create local project project_path = os.path.join(cwd, "test_project") mkdir(project_path) mkdir(os.path.join(project_path, "trunk")) hello_relative_path = os.path.join("trunk", "hello.txt") hello_path = os.path.join(project_path, hello_relative_path) echo_to_file(hello_path, "hello") # --- create a SVN repository from that project repos_path = os.path.join(cwd, "repos") project_url = SVNRepository.create(repo_path=repos_path) project_url = project_url + "/Test_Project" p = Run([ "svn", "import", unixpath(project_path), project_url, "-m", "initial import" ]) assert p.status == 0, p.out # --- checkout project into working dir A working_copy_a_path = os.path.join(cwd, "working_copy_a") svn_a = SVNRepository(working_copy_a_path) with pytest.raises(SVNError): svn_a.update() with pytest.raises(SVNError): svn_a.update(url=file_url("bad_url")) local_change = svn_a.update(project_url) assert local_change local_change = svn_a.update() assert not local_change # verify the content of the working dir A and its revision assert svn_a.url == project_url assert os.path.exists( os.path.join(working_copy_a_path, hello_relative_path)), "checkout failed" assert svn_a.current_revision == "1" # modify the working dir, commit the change, # update the working dir and verify the new current revision echo_to_file(os.path.join(working_copy_a_path, hello_relative_path), "bye") svn_a.svn_cmd(["commit", "-m", "modify hello"]) svn_a.update() assert svn_a.current_revision == "2" svn_a.update(revision="1") assert svn_a.current_revision == "1" with pytest.raises(SVNError): svn_a.update(revision="404") # make local changes in the working dir B before updating it working_copy_b_path = os.path.join(cwd, "working_copy_b") svn_b = SVNRepository(working_copy_b_path) svn_b.update(project_url) foo_path = os.path.join(working_copy_b_path, "trunk", "foo") touch(foo_path) hello_b_path = os.path.join(working_copy_b_path, hello_relative_path) echo_to_file(hello_b_path, "kitty") local_change = svn_b.update() assert local_change assert os.path.exists(foo_path) with open(hello_b_path) as f: assert "kitty" in f.read() # update and cancel all changes svn_b.update(force_and_clean=True) assert not os.path.exists(foo_path) with open(hello_b_path) as f: assert "bye" in f.read() # checkout into an existing path (not versioned) working_copy_c_path = os.path.join(cwd, "working_copy_c") svn_c = SVNRepository(working_copy_c_path) touch(working_copy_c_path) with pytest.raises(SVNError) as err: svn_c.update(url=project_url) assert "not empty" in str(err) rm(working_copy_c_path) mkdir(working_copy_c_path) bar_path = os.path.join(working_copy_c_path, "bar") touch(bar_path) # verify failures without the force option with pytest.raises(SVNError) as err: svn_c.update(url=project_url) assert "not empty" in str(err) touch(os.path.join(working_copy_c_path, ".svn")) with pytest.raises(SVNError): svn_c.update(url=project_url) svn_c.update(url=project_url, force_and_clean=True) # verify that the working dir C is clean assert not os.path.exists(bar_path) # modify a working dir and update it with a new project svn_d = SVNRepository(working_copy_c_path) touch(bar_path) svn_d.update() # update with the last URL used for this dir svn_d.update(url=project_url) # update with the same URL assert os.path.exists(bar_path) project2_url = project_url + "2" p = Run([ "svn", "import", unixpath(project_path), project2_url, "-m", "initial import" ]) assert p.status == 0, p.out with pytest.raises(SVNError) as err: svn_d.update(url=project2_url) # update with new URL assert "not empty" in str(err) svn_d.update(url=project2_url, force_and_clean=True) assert svn_d.url == project2_url
def run(self, previous_values, slot): # Check whether the test should be skipped skip = self.should_skip() if skip is not None: self.result.set_status(skip) self.push_result() return False # If there's a test.cmd, execute it with the shell; # otherwise execute test.py. wd = self.test_env["working_dir"] base = os.path.abspath( os.path.join(os.path.dirname(__file__), "..", "..")) # In the development environment, run the development GPS, # otherwise use the GS found on the PATH devel_gs = os.path.join(base, "gnatstudio", "obj", "gnatstudio") if sys.platform == "win32": devel_gs += ".exe" devel_gs = unixpath(devel_gs) test_cmd = os.path.join(wd, "test.cmd") if os.path.exists(devel_gs): # We are testing the development executable: we need to # pass the valgrind command ourselves. if os.path.exists(test_cmd): # run via a test.cmd GS = " ".join(self.env.valgrind_cmd + [devel_gs]) cmd_line = ["bash", test_cmd] else: # run the executable directly GS = devel_gs cmd_line = self.env.valgrind_cmd + [ devel_gs, "--load=python:test.py" ] else: # We are testing the real 'gnatstudio' script. # In this case we rely on GPS_WRAPPER to carry the # valgrind command. GS = "gnatstudio" if os.path.exists(test_cmd): # run via a test.cmd cmd_line = ["bash", test_cmd] else: # run the script directly cmd_line = [GS, "--load=python:test.py"] env = { "GNATSTUDIO_HOME": self.test_env["working_dir"], "GNATINSPECT": shutil.which("gnatinspect") + " --exit", "GNATSTUDIO": GS, "GPS": GS, "GPS_WRAPPER": " ".join(self.env.valgrind_cmd), "GNATSTUDIO_PYTHON_COV": self.test_env["pycov"], } env.update(Xvfbs.get_env(slot)) process = Run( cmd_line, cwd=wd, timeout=(None if "GPS_PREVENT_EXIT" in os.environ else (120 * self.env.wait_factor)), env=env, ignore_environ=False, ) output = process.out if output: # If there's an output, capture it self.result.log += output is_error = False if process.status: # Nonzero status? if process.status == 100: # This one is an xfail self.result.set_status(TestStatus.XFAIL) elif process.status == 99: # This is intentionally deactivated in this configuration self.result.set_status(TestStatus.SKIP) else: # Unknown status! self.result.set_status(TestStatus.ERROR) is_error = True else: # Status is 0... if output: # ... and there is an output: compare it to test.out # if it exists test_out = os.path.join(wd, "test.out") if os.path.exists(test_out): with open(test_out, "r") as f: expected = f.read() res = "\n".join( difflib.unified_diff(expected.splitlines(), output.splitlines())) if res == "": self.result.set_status(TestStatus.PASS) else: self.result.out = Log(output) self.result.expected = Log(expected) self.result.diff = Log(res) self.result.set_status(TestStatus.FAIL) is_error = True else: # ... if there's no test.out, that's a FAIL self.result.set_status(TestStatus.FAIL) is_error = True else: # ... and no output: that's a PASS self.result.set_status(TestStatus.PASS) if is_error: self.result.log += self._capture_for_developers() self.push_result()
def unixpath_to(pgmname): """ Return the absolute path to the executable file expected in the current directory for a main subprogram PGMNAME, unixified. """ return unixpath(os.path.abspath(exename_for(pgmname)))
def test_git_repo(): working_tree = os.path.join(os.getcwd(), "working_tree") working_tree2 = os.path.join(os.getcwd(), "working_tree2") repo = GitRepository(working_tree) repo.init() os.chdir(working_tree) new_file = os.path.join(working_tree, "new.txt") echo_to_file(new_file, "new\n") commit_note = unixpath(os.path.join(working_tree, "commit_note.txt")) echo_to_file( commit_note, "\n".join(( "Code-Review+2: Nobody <*****@*****.**>", "Submitted-by: Nobody <*****@*****.**>", "Submitted-at: Thu, 08 Jun 2017 18:40:11 +0200", )), ) repo.git_cmd(["add", "new.txt"]) repo.git_cmd(["config", "user.email", "*****@*****.**"]) repo.git_cmd(["config", "user.name", "e3 core"]) repo.git_cmd(["commit", "-m", "new file"]) repo.git_cmd(["tag", "-a", "-m", "new tag", "20.0855369232"]) repo.git_cmd( ["notes", "--ref", "review", "add", "HEAD", "-F", commit_note]) # try with gerrit notes with open("log.txt", "w") as f: repo.write_log(f, with_gerrit_notes=True) with open("log.txt") as f: commits = list(repo.parse_log(f)) assert "*****@*****.**" in commits[0]["notes"]["Code-Review+2"] # try with an invalid note repo.git_cmd([ "notes", "--ref", "review", "add", "HEAD", "-f", "-m", "invalid-note" ]) with open("log.txt", "w") as f: repo.write_log(f, with_gerrit_notes=True) with open("log.txt") as f: commits = list(repo.parse_log(f)) assert commits[0]["notes"] is None # try again without gerrit notes with open("log.txt", "w") as f: repo.write_log(f) with open("log.txt") as f: commits = list(repo.parse_log(f)) assert "new file" in commits[0]["message"] assert commits[0]["email"] == "*****@*****.**" new_sha = commits[0]["sha"] assert "20.0855369232" in repo.describe() assert new_sha == repo.rev_parse() with pytest.raises(GitError) as err: repo.describe("g") assert "describe --always g" in str(err) echo_to_file(new_file, "new line\n", append=True) with open("commit1.diff", "wb") as f: repo.write_local_diff(f) with open("commit1.diff", "rb") as f: assert b"+new line" in f.read() echo_to_file(new_file, 10000 * "*") repo.git_cmd(["commit", "-a", "-m", "file update"]) with open("log2.txt", "w") as f: repo.write_log(f) with open("log2.txt") as f: commits = list(repo.parse_log(f, max_diff_size=1000)) # assert b'diff too long' not in commits[1]['diff'] assert "file update" in commits[0]["message"] assert "diff too long" in commits[0]["diff"] assert "new file" in commits[1]["message"] assert commits[1]["sha"] != commits[0]["sha"] assert commits[1]["diff"] != commits[0]["diff"] repo2 = GitRepository(working_tree2) giturl = "file://%s" % working_tree.replace("\\", "/") repo2.init(url=giturl, remote="tree1") repo2.update(url=giturl, refspec="master") repo2.rev_parse() == repo.rev_parse() repo2.fetch_gerrit_notes(url=giturl) p = repo2.git_cmd(["notes", "--ref=review", "show", new_sha], output=subprocess.PIPE) assert "invalid-note" in p.out