def test_is_dir_empty(): work_dir = os.getcwd() test_dir_path = os.path.join(work_dir, 'dir') deleted_file_path = os.path.join(test_dir_path, 'deleted2.txt') deleted_file2_path = os.path.join(test_dir_path, 'deleted.txt') mkdir(test_dir_path) ntfile = NTFile(test_dir_path) ntfile2 = NTFile(deleted_file_path) try: assert ntfile.is_dir_empty touch(deleted_file_path) touch(deleted_file2_path) assert not ntfile.is_dir_empty ntfile2.open(Access.DELETE, Share.DELETE) ntfile2.dispose() assert not ntfile.is_dir_empty rm(deleted_file2_path) assert ntfile.is_dir_empty finally: ntfile.close() ntfile2.close()
def test_iterate_on_dir(): work_dir = os.getcwd() test_dir_path = os.path.join(work_dir, 'dir') mkdir(test_dir_path) result = set() def fun(name, ntfile_instance): result.add(name) return True, False try: ntfile = NTFile(test_dir_path) status = ntfile.iterate_on_dir(fun, default_result=False) assert not result assert not status finally: ntfile.close() for n in range(0, 40): touch(os.path.join(test_dir_path, '%s.txt' % n)) try: ntfile = NTFile(test_dir_path) status = ntfile.iterate_on_dir(fun, default_result=False) assert status assert len(result) == 40 finally: ntfile.close()
def test_volume_path(): work_dir = os.getcwd() test_file_path = os.path.join(work_dir, 'test_vpath.txt') touch(test_file_path) ntfile = NTFile(test_file_path) assert ntfile.volume_path with pytest.raises(NTException): # Choose a volume name that is unlikely to exist 0:/ ntfile = NTFile('0:/dummy') ntfile.volume_path
def test_read_attributes(): work_dir = os.getcwd() test_file_path = os.path.join(work_dir, 'test_read_attr_file.txt') touch(test_file_path) ntfile = NTFile(test_file_path) ntfile.read_attributes() # On appveyor calling as_datetime fails because # ntfile.basic_info.change_time is equal to 0. Ignore this until # we have a real reproducer assert datetime.now() - \ ntfile.basic_info.change_time.as_datetime < \ timedelta(seconds=10)
def test_rename(): work_dir = os.getcwd() test_file_path = os.path.join(work_dir, 'test_rename.txt') touch(test_file_path) ntfile = NTFile(test_file_path) ntfile.open(Access.READ_DATA) try: with pytest.raises(NTException): ntfile.rename(os.path.join(work_dir, 'test_rename2.txt')) finally: ntfile.close() ntfile.rename(os.path.join(work_dir, 'test_rename2.txt'))
def test_uid(): work_dir = os.getcwd() test_file_path = os.path.join(work_dir, 'test_uid.txt') touch(test_file_path) ntfile = NTFile(test_file_path) ntfile.read_attributes() assert ntfile.uid > 0 ntfile2 = NTFile(os.path.join(work_dir, 'non_existing.txt')) with pytest.raises(NTException): ntfile2.uid
def test_open_file_in_dir(): work_dir = os.getcwd() test_dir_path = os.path.join(work_dir, 'dir') mkdir(test_dir_path) touch(os.path.join(test_dir_path, 'toto.txt')) try: ntfile = NTFile(test_dir_path) ntfile.open() ntfile2 = NTFile('toto.txt', parent=ntfile) ntfile2.open() finally: ntfile.close() ntfile2.close()
def test_move_to_trash(): work_dir = os.getcwd() test_file_path = os.path.join(work_dir, 'test_mv_to_trash.txt') touch(test_file_path) ntfile = NTFile(test_file_path) ntfile.open(Access.READ_DATA) try: with pytest.raises(NTException): ntfile.move_to_trash() finally: ntfile.close() trash_path = ntfile.trash_path ntfile.move_to_trash() rm(trash_path)
def test_unlink(): work_dir = os.getcwd() test_dir_path = os.path.join(work_dir, 'dir') deleted_file_path = os.path.join(test_dir_path, 'deleted2.txt') mkdir(test_dir_path) ntfile = NTFile(test_dir_path) ntfile3 = NTFile(test_dir_path) ntfile2 = NTFile(deleted_file_path) try: # delete inexisting file ntfile2.unlink() # delete file with readonly attribute touch(deleted_file_path) ntfile2.read_attributes() ntfile2.basic_info.file_attributes.attr |= FileAttribute.READONLY ntfile2.write_attributes() ntfile2.unlink() # delete file already pending deletion touch(deleted_file_path) ntfile2.open(Access.DELETE, Share.DELETE) ntfile2.dispose() ntfile2.unlink() # delete containing directory ntfile.unlink() ntfile.close() ntfile2.close() mkdir(test_dir_path) ntfile.open(Access.LIST_DIRECTORY, Share.ALL) ntfile3.unlink() finally: ntfile.close() ntfile2.close() ntfile2.close()
def test_write_attributes(): work_dir = os.getcwd() test_file_path = os.path.join(work_dir, 'test_write_attr_file.txt') touch(test_file_path) ntfile = NTFile(test_file_path) ntfile.read_attributes() ntfile.open(Access.READ_ATTRS) ntfile.basic_info.change_time = FileTime( datetime.now() - timedelta(seconds=3600)) try: with pytest.raises(NTException): ntfile.write_attributes() finally: ntfile.close() ntfile.basic_info.change_time = FileTime( datetime.now() - timedelta(days=3)) ntfile.write_attributes() assert datetime.now() - \ ntfile.basic_info.change_time.as_datetime > \ timedelta(seconds=3000)
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 = file_url(repos_path + '/Test_project') p = Run(['svnadmin', 'create', repos_path]) assert p.status == 0, p.out p = Run( ['svn', 'import', 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, 'r') 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, 'r') 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', 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 prepare_src(repos, dest): touch(os.path.join(dest, 'my_generated_source_file'))
import re import os import shutil import subprocess # "remote" is going to be the remote crate init_local_crate(name="remote", enter=False) url = os.path.join(os.getcwd(), "remote") head1 = init_git_repo("remote") os.chdir("remote") default_branch = git_branch() # Create a second branch and commit for testing subprocess.run(["git", "checkout", "-b", "devel"]).check_returncode() touch("telltale") subprocess.run(["git", "add", "telltale"]).check_returncode() subprocess.run(["git", "commit", "-m", "branching"]).check_returncode() os.chdir("..") # Now pin to the branch, and verify the telltale file exists in the checkout init_local_crate() alr_pin("remote", url=url, branch="devel") p = run_alr("pin") assert_match("remote file:alire/cache/pins/remote " + re.escape(url) + "#devel\n", # branch in the info matches p.out) # Also verify the file exists assert os.path.exists("alire/cache/pins/remote/telltale"), \ "Missing file in checkout"
from e3.fs import mkdir from e3.os.fs import touch mkdir("process_dir") touch("process_dir/hello.txt")
def test_unlink(): work_dir = os.getcwd() test_dir_path = os.path.join(work_dir, "dir") deleted_file_path = os.path.join(test_dir_path, "deleted2.txt") mkdir(test_dir_path) ntfile = NTFile(test_dir_path) ntfile3 = NTFile(test_dir_path) ntfile2 = NTFile(deleted_file_path) try: # delete inexisting file ntfile2.unlink() # delete file with readonly attribute touch(deleted_file_path) ntfile2.read_attributes() ntfile2.basic_info.file_attributes.attr |= FileAttribute.READONLY assert "READONLY" in str(ntfile2.basic_info.file_attributes) ntfile2.write_attributes() ntfile2.unlink() # delete file already pending deletion touch(deleted_file_path) ntfile2.open(Access.DELETE, Share.DELETE) ntfile2.dispose() ntfile2.unlink() # delete containing directory ntfile.unlink() ntfile.close() ntfile2.close() mkdir(test_dir_path) ntfile.open(Access.LIST_DIRECTORY, Share.ALL) ntfile3.unlink() finally: ntfile.close() ntfile2.close() ntfile2.close() ntfile = NTFile("nul") with pytest.raises(NTException) as err: ntfile.unlink() ntfile.close() assert "NTFile.read_attributes_internal:" in str(err.value) # A directory that is not empty cannot be deleted dir_to_delete = os.path.join(test_dir_path, "dir_to_delete") mkdir(dir_to_delete) touch(os.path.join(dir_to_delete, "afile.txt")) ntfile = NTFile(dir_to_delete) try: with pytest.raises(NTException) as err: ntfile.unlink() finally: ntfile.close() # A directory that is already opened and not empty cannot be # moved to trash dir_to_delete = os.path.join(test_dir_path, "dir_to_delete") mkdir(dir_to_delete) touch(os.path.join(dir_to_delete, "afile.txt")) ntfile = NTFile(dir_to_delete) ntfile2 = NTFile(dir_to_delete) try: ntfile.open(Access.LIST_DIRECTORY, Share.ALL) with pytest.raises(NTException) as err: ntfile2.unlink() finally: ntfile.close() ntfile2.close() # Try to delete a file that we cannot open ntfile = NTFile(deleted_file_path) ntfile2 = NTFile(deleted_file_path) try: touch(deleted_file_path) ntfile.open(Access.READ_DATA, Share.NOTHING) with pytest.raises(NTException) as err: ntfile2.unlink() finally: ntfile.close() ntfile2.close()
def test_svn_checkout(self, compute_changelog, e3_feature): os.environ["GIT_AUTHOR_EMAIL"] = "*****@*****.**" os.environ["GIT_AUTHOR_NAME"] = "e3 core" os.environ["GIT_COMMITTER_NAME"] = "*****@*****.**" os.environ["GIT_COMMITTER_EMAIL"] = "e3 core" os.environ["E3_ENABLE_FEATURE"] = e3_feature url = SVNRepository.create("svn", initial_content_path=self.repo_data) url2 = SVNRepository.create("svn2", initial_content_path=self.repo_data2) url3 = GitRepository.create("git", initial_content_path=self.repo_data) url4 = GitRepository.create("git2", initial_content_path=self.repo_data2) r = SVNRepository(working_copy=os.path.abspath("svn_checkout")) r.update(url=url) m = CheckoutManager(name="myrepo", working_dir=".", compute_changelog=compute_changelog) logging.info("update of non existing url") result = m.update(vcs="svn", url=url + "wrong_url") assert result == ReturnValue.failure logging.info("update of existing url") result = m.update(vcs="svn", url=url) assert result == ReturnValue.success logging.info("update of existing url with no changes") result = m.update(vcs="svn", url=url) assert result == ReturnValue.unchanged # Update the repository logging.info("Do a checkin in svn repository") with open(os.path.join("svn_checkout", "file3.txt"), "w") as fd: fd.write("new file!") r.svn_cmd(["add", "file3.txt"]) r.svn_cmd(["commit", "file3.txt", "-m", "checkin"]) logging.info("Check that we see the update") result = m.update(vcs="svn", url=url) assert result == ReturnValue.success assert os.path.isfile(os.path.join("svn_checkout", "file3.txt")) logging.info("Do a local modification in the working dir") with open(os.path.join("myrepo", "file3.txt"), "w") as fd: fd.write("new file modified!") logging.info("And then do an update and check that cleanup was done") result = m.update(vcs="svn", url=url) assert result == ReturnValue.unchanged with open(os.path.join("myrepo", "file3.txt")) as fd: assert fd.read().strip() == "new file!" logging.info("Check that we can switch from one svn url to another") result = m.update(vcs="svn", url=url2) assert result == ReturnValue.success assert os.path.isfile(os.path.join("myrepo", "file1.txt", "data2.txt")) logging.info("Check that we can switch from one svn url to a git repo") result = m.update(vcs="git", url=url3, revision="master") assert result == ReturnValue.success assert os.path.isfile(os.path.join("myrepo", "file1.txt")) logging.info( "Check that we can switch from one git url to another one") result = m.update(vcs="git", url=url4, revision="master") assert result == ReturnValue.success assert os.path.isfile(os.path.join("myrepo", "file1.txt", "data2.txt")) logging.info("Check that in case of no changes unchanged is returned") result = m.update(vcs="git", url=url4, revision="master") assert result == ReturnValue.unchanged logging.info("Check that changes are detected in git reposotories") with open(os.path.join("git2", "file3.txt"), "w") as fd: fd.write("new file!") r = GitRepository(os.path.abspath("git2")) r.git_cmd(["add", "file3.txt"]) r.git_cmd(["commit", "-m", "new file"]) result = m.update(vcs="git", url=url4, revision="master") assert result == ReturnValue.success assert os.path.isfile(os.path.join("myrepo", "file3.txt")) logging.info("Check that local modifications are discarded") with open(os.path.join("myrepo", "file3.txt"), "w") as fd: fd.write("new file modified!") result = m.update(vcs="git", url=url4, revision="master") assert result == ReturnValue.unchanged with open(os.path.join("myrepo", "file3.txt")) as fd: assert fd.read().strip() == "new file!" result = m.update(vcs="git", url=url4 + "non-existing", revision="master") assert result == ReturnValue.failure result = m.update(vcs="git", url=url4) assert result == ReturnValue.failure # Add a .gitignore and use an external touch("git2/file4.txt") touch("git2/ignore_file.txt") with open("git2/.gitignore", "w") as fd: fd.write("/ignore_file.txt") result = m.update(vcs="external", url=os.path.abspath("git2")) assert os.path.isfile(os.path.join(m.working_dir, "file4.txt")) assert not os.path.isfile( os.path.join(m.working_dir, "ignore_file.txt")) assert result == ReturnValue.success result = m.update(vcs="external", url=os.path.abspath("git2")) assert result == ReturnValue.unchanged
from e3.fs import mkdir from e3.os.fs import touch import os mkdir("test1") for f in range(1000): touch(os.path.join("test1", str(f"file-{f}"))) for f in range(1000): mkdir(os.path.join("test1", str(f"dir-{f}")))
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", 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, "r") 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, "r") 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", 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
import re import os import shutil import subprocess # "remote" is going to be the remote crate. Two other crates will depend on it # with different branches/commits etc init_local_crate(name="zzz", enter=False) url = os.path.join(os.getcwd(), "zzz") head1 = init_git_repo("zzz") os.chdir("zzz") # Create a second branch and commit for testing subprocess.run(["git", "checkout", "-b", "devel"]).check_returncode() touch("x") subprocess.run(["git", "add", "x"]).check_returncode() subprocess.run(["git", "commit", "-m", "branching"]).check_returncode() head2 = git_head() os.chdir("..") # In this function we will test that two crates with the same remote works, # for several combinations def should_work(commit="", branch=""): os.mkdir("nest") os.chdir("nest") for crate in ["xxx", "yyy"]: init_local_crate(crate) alr_pin("zzz", url=url, commit=commit, branch=branch)