<commit_before>import py
import sys
from py.path import local
from testing.path import common

failsonjython = py.test.mark.xfail("sys.platform.startswith('java')")

def pytest_funcarg__path1(request):
    def setup():
        path1 = request.config.mktemp("path1")
        common.setuptestfs(path1)
        return path1
    def teardown(path1):
        # post check 
        assert path1.join("samplefile").check()
    return request.cached_setup(setup, teardown, scope="session")

class TestLocalPath(common.CommonFSTests):
    def test_join_normpath(self, tmpdir):
        assert tmpdir.join(".") == tmpdir
        p = tmpdir.join("../%s" % tmpdir.basename)
        assert p == tmpdir
        p = tmpdir.join("..//%s/" % tmpdir.basename)
        assert p == tmpdir

    def test_gethash(self, tmpdir):
        md5 = py.builtin._tryimport('md5', 'hashlib').md5
        lib = py.builtin._tryimport('sha', 'hashlib')
        sha = getattr(lib, 'sha1', getattr(lib, 'sha', None))
        fn = tmpdir.join("testhashfile")
        data = 'hello'.encode('ascii')
        fn.write(data, mode="wb")
        assert fn.computehash("md5") == md5(data).hexdigest()
        assert fn.computehash("sha1") == sha(data).hexdigest()
        py.test.raises(ValueError, fn.computehash, "asdasd")

    def test_remove_removes_readonly_file(self, tmpdir):
        readonly_file = tmpdir.join('readonly').ensure()
        readonly_file.chmod(0)
        readonly_file.remove()
        assert not readonly_file.check(exists=1)

    def test_remove_removes_readonly_dir(self, tmpdir):
        readonly_dir = tmpdir.join('readonlydir').ensure(dir=1)
        readonly_dir.chmod(int("500", 8))
        readonly_dir.remove()
        assert not readonly_dir.check(exists=1)

    def test_remove_removes_dir_and_readonly_file(self, tmpdir):
        readonly_dir = tmpdir.join('readonlydir').ensure(dir=1)
        readonly_file = readonly_dir.join('readonlyfile').ensure()
        readonly_file.chmod(0)
        readonly_dir.remove()
        assert not readonly_dir.check(exists=1)

    def test_initialize_curdir(self):
        assert str(local()) == py.std.os.getcwd()

    def test_initialize_reldir(self, path1):
        old = path1.chdir()
        try:
            p = local('samplefile')
            assert p.check()
        finally:
            old.chdir()

    def test_eq_with_strings(self, path1):
        path1 = path1.join('sampledir')
        path2 = str(path1)
        assert path1 == path2
        assert path2 == path1
        path3 = path1.join('samplefile')
        assert path3 != path2
        assert path2 != path3

    @py.test.mark.multi(bin=(False, True))
    def test_dump(self, tmpdir, bin):
        path = tmpdir.join("dumpfile%s" % int(bin))
        try:
            d = {'answer' : 42}
            path.dump(d, bin=bin)
            f = path.open('rb+')
            dnew = py.std.pickle.load(f)
            assert d == dnew
        finally:
            f.close()

    def test_setmtime(self):
        import tempfile
        import time
        try:
            fd, name = tempfile.mkstemp()
            py.std.os.close(fd)
        except AttributeError:
            name = tempfile.mktemp()
            open(name, 'w').close()
        try:
            mtime = int(time.time())-100
            path = local(name)
            assert path.mtime() != mtime
            path.setmtime(mtime)
            assert path.mtime() == mtime
            path.setmtime()
            assert path.mtime() != mtime
        finally:
            py.std.os.remove(name)

    def test_normpath(self, path1):
        new1 = path1.join("/otherdir")
        new2 = path1.join("otherdir")
        assert str(new1) == str(new2)

    def test_mkdtemp_creation(self):
        d = local.mkdtemp()
        try:
            assert d.check(dir=1)
        finally:
            d.remove(rec=1)

    def test_tmproot(self):
        d = local.mkdtemp()
        tmproot = local.get_temproot()
        try:
            assert d.check(dir=1)
            assert d.dirpath() == tmproot
        finally:
            d.remove(rec=1)

    def test_chdir(self, tmpdir):
        old = local()
        try:
            res = tmpdir.chdir()
            assert str(res) == str(old)
            assert py.std.os.getcwd() == str(tmpdir)
        finally:
            old.chdir()

    def test_ensure_filepath_withdir(self, tmpdir):
        newfile = tmpdir.join('test1','test')
        newfile.ensure()
        assert newfile.check(file=1)
        newfile.write("42")
        newfile.ensure()
        s = newfile.read()
        assert s == "42"

    def test_ensure_filepath_withoutdir(self, tmpdir):
        newfile = tmpdir.join('test1file')
        t = newfile.ensure()
        assert t == newfile
        assert newfile.check(file=1)

    def test_ensure_dirpath(self, tmpdir):
        newfile = tmpdir.join('test1','testfile')
        t = newfile.ensure(dir=1)
        assert t == newfile
        assert newfile.check(dir=1)

    def test_init_from_path(self, tmpdir):
        l = local()
        l2 = local(l)
        assert l2 is l

        wc = py.path.svnwc('.')
        l3 = local(wc)
        assert l3 is not wc
        assert l3.strpath == wc.strpath
        assert not hasattr(l3, 'commit')

    def test_long_filenames(self, tmpdir):
        if sys.platform == "win32":
            py.test.skip("win32: work around needed for path length limit")
        # see http://codespeak.net/pipermail/py-dev/2008q2/000922.html
        
        # testing paths > 260 chars (which is Windows' limitation, but
        # depending on how the paths are used), but > 4096 (which is the
        # Linux' limitation) - the behaviour of paths with names > 4096 chars
        # is undetermined
        newfilename = '/test' * 60
        l = tmpdir.join(newfilename)
        l.ensure(file=True)
        l.write('foo')
        l2 = tmpdir.join(newfilename)
        assert l2.read() == 'foo'

    def test_visit_depth_first(self, tmpdir):
        p1 = tmpdir.ensure("a","1")
        p2 = tmpdir.ensure("b","2")
        p3 = tmpdir.ensure("breadth")
        l = list(tmpdir.visit(lambda x: x.check(file=1)))
        assert l[0] == p1
        assert l[1] == p2 
        assert l[2] == p3

class TestExecutionOnWindows:
    pytestmark = py.test.mark.skipif("sys.platform != 'win32'")

    def test_sysfind(self):
        x = py.path.local.sysfind('cmd')
        assert x.check(file=1)
        assert py.path.local.sysfind('jaksdkasldqwe') is None

class TestExecution:
    pytestmark = py.test.mark.skipif("sys.platform == 'win32'")

    def test_sysfind(self):<commit_msg>avoid dependency on directory ordering<commit_after>import py
import sys
from py.path import local
from testing.path import common

failsonjython = py.test.mark.xfail("sys.platform.startswith('java')")

def pytest_funcarg__path1(request):
    def setup():
        path1 = request.config.mktemp("path1")
        common.setuptestfs(path1)
        return path1
    def teardown(path1):
        # post check 
        assert path1.join("samplefile").check()
    return request.cached_setup(setup, teardown, scope="session")

class TestLocalPath(common.CommonFSTests):
    def test_join_normpath(self, tmpdir):
        assert tmpdir.join(".") == tmpdir
        p = tmpdir.join("../%s" % tmpdir.basename)
        assert p == tmpdir
        p = tmpdir.join("..//%s/" % tmpdir.basename)
        assert p == tmpdir

    def test_gethash(self, tmpdir):
        md5 = py.builtin._tryimport('md5', 'hashlib').md5
        lib = py.builtin._tryimport('sha', 'hashlib')
        sha = getattr(lib, 'sha1', getattr(lib, 'sha', None))
        fn = tmpdir.join("testhashfile")
        data = 'hello'.encode('ascii')
        fn.write(data, mode="wb")
        assert fn.computehash("md5") == md5(data).hexdigest()
        assert fn.computehash("sha1") == sha(data).hexdigest()
        py.test.raises(ValueError, fn.computehash, "asdasd")

    def test_remove_removes_readonly_file(self, tmpdir):
        readonly_file = tmpdir.join('readonly').ensure()
        readonly_file.chmod(0)
        readonly_file.remove()
        assert not readonly_file.check(exists=1)

    def test_remove_removes_readonly_dir(self, tmpdir):
        readonly_dir = tmpdir.join('readonlydir').ensure(dir=1)
        readonly_dir.chmod(int("500", 8))
        readonly_dir.remove()
        assert not readonly_dir.check(exists=1)

    def test_remove_removes_dir_and_readonly_file(self, tmpdir):
        readonly_dir = tmpdir.join('readonlydir').ensure(dir=1)
        readonly_file = readonly_dir.join('readonlyfile').ensure()
        readonly_file.chmod(0)
        readonly_dir.remove()
        assert not readonly_dir.check(exists=1)

    def test_initialize_curdir(self):
        assert str(local()) == py.std.os.getcwd()

    def test_initialize_reldir(self, path1):
        old = path1.chdir()
        try:
            p = local('samplefile')
            assert p.check()
        finally:
            old.chdir()

    def test_eq_with_strings(self, path1):
        path1 = path1.join('sampledir')
        path2 = str(path1)
        assert path1 == path2
        assert path2 == path1
        path3 = path1.join('samplefile')
        assert path3 != path2
        assert path2 != path3

    @py.test.mark.multi(bin=(False, True))
    def test_dump(self, tmpdir, bin):
        path = tmpdir.join("dumpfile%s" % int(bin))
        try:
            d = {'answer' : 42}
            path.dump(d, bin=bin)
            f = path.open('rb+')
            dnew = py.std.pickle.load(f)
            assert d == dnew
        finally:
            f.close()

    def test_setmtime(self):
        import tempfile
        import time
        try:
            fd, name = tempfile.mkstemp()
            py.std.os.close(fd)
        except AttributeError:
            name = tempfile.mktemp()
            open(name, 'w').close()
        try:
            mtime = int(time.time())-100
            path = local(name)
            assert path.mtime() != mtime
            path.setmtime(mtime)
            assert path.mtime() == mtime
            path.setmtime()
            assert path.mtime() != mtime
        finally:
            py.std.os.remove(name)

    def test_normpath(self, path1):
        new1 = path1.join("/otherdir")
        new2 = path1.join("otherdir")
        assert str(new1) == str(new2)

    def test_mkdtemp_creation(self):
        d = local.mkdtemp()
        try:
            assert d.check(dir=1)
        finally:
            d.remove(rec=1)

    def test_tmproot(self):
        d = local.mkdtemp()
        tmproot = local.get_temproot()
        try:
            assert d.check(dir=1)
            assert d.dirpath() == tmproot
        finally:
            d.remove(rec=1)

    def test_chdir(self, tmpdir):
        old = local()
        try:
            res = tmpdir.chdir()
            assert str(res) == str(old)
            assert py.std.os.getcwd() == str(tmpdir)
        finally:
            old.chdir()

    def test_ensure_filepath_withdir(self, tmpdir):
        newfile = tmpdir.join('test1','test')
        newfile.ensure()
        assert newfile.check(file=1)
        newfile.write("42")
        newfile.ensure()
        s = newfile.read()
        assert s == "42"

    def test_ensure_filepath_withoutdir(self, tmpdir):
        newfile = tmpdir.join('test1file')
        t = newfile.ensure()
        assert t == newfile
        assert newfile.check(file=1)

    def test_ensure_dirpath(self, tmpdir):
        newfile = tmpdir.join('test1','testfile')
        t = newfile.ensure(dir=1)
        assert t == newfile
        assert newfile.check(dir=1)

    def test_init_from_path(self, tmpdir):
        l = local()
        l2 = local(l)
        assert l2 is l

        wc = py.path.svnwc('.')
        l3 = local(wc)
        assert l3 is not wc
        assert l3.strpath == wc.strpath
        assert not hasattr(l3, 'commit')

    def test_long_filenames(self, tmpdir):
        if sys.platform == "win32":
            py.test.skip("win32: work around needed for path length limit")
        # see http://codespeak.net/pipermail/py-dev/2008q2/000922.html
        
        # testing paths > 260 chars (which is Windows' limitation, but
        # depending on how the paths are used), but > 4096 (which is the
        # Linux' limitation) - the behaviour of paths with names > 4096 chars
        # is undetermined
        newfilename = '/test' * 60
        l = tmpdir.join(newfilename)
        l.ensure(file=True)
        l.write('foo')
        l2 = tmpdir.join(newfilename)
        assert l2.read() == 'foo'

    def test_visit_depth_first(self, tmpdir):
        p1 = tmpdir.ensure("a","1")
        p2 = tmpdir.ensure("b","2")
        p3 = tmpdir.ensure("breadth")
        l = list(tmpdir.visit(lambda x: x.check(file=1)))
        assert len(l) == 3
        # check that breadth comes last
        assert l[2] == p3 

class TestExecutionOnWindows:
    pytestmark = py.test.mark.skipif("sys.platform != 'win32'")

    def test_sysfind(self):
        x = py.path.local.sysfind('cmd')
        assert x.check(file=1)
        assert py.path.local.sysfind('jaksdkasldqwe') is None

class TestExecution:
    pytestmark = py.test.mark.skipif("sys.platform == 'win32'")

    def test_sysfind(self):
{'fraction_non_alphanumeric': 0.12730153939028072, 'fraction_numerical': 0.01818593419861153, 'mean_word_length': 2.1547250654606045, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3596891', 'n_tokens_mistral': 4385, 'n_tokens_neox': 4263, 'n_words': 871}