コード例 #1
0
    def test_findfile(self):
        self.tmpdir = os.path.abspath(tempfile.mkdtemp())
        subdir_name = 'aaa'
        subdir = os.path.join(self.tmpdir, subdir_name)
        os.mkdir(subdir)
        os.chdir(self.tmpdir)

        fname = 'foo.py'
        self.assertEqual(None, find_file(fname))

        open(os.path.join(self.tmpdir, fname), 'w').close()
        open(os.path.join(subdir, fname), 'w').close()
        open(os.path.join(subdir, 'aaa'), 'w').close()
        # we can find files in the CWD
        self.assertEqual(os.path.join(self.tmpdir, fname), find_file(fname))
        # unless we don't look in the cwd
        self.assertEqual(None, find_file(fname, cwd=False))
        # cwd overrides pathlist
        self.assertEqual(os.path.join(self.tmpdir, fname),
                         find_file(fname, pathlist=[subdir]))
        self.assertEqual(os.path.join(subdir, fname),
                         find_file(fname, pathlist=[subdir], cwd=False))
        # ...unless the CWD match fails the MODE check
        #  (except on Windows, where all files have X_OK)
        self.assertEqual((os.path.join(self.tmpdir, fname)
                          if _system() in ('windows', 'cygwin') else None),
                         find_file(fname, pathlist=[subdir], mode=os.X_OK))
        self._make_exec(os.path.join(subdir, fname))
        self.assertEqual(
            (os.path.join(self.tmpdir, fname) if _system()
             in ('windows', 'cygwin') else os.path.join(subdir, fname)),
            find_file(fname, pathlist=[subdir], mode=os.X_OK))
        # pathlist may also be a string
        self.assertEqual(
            os.path.join(subdir, fname),
            find_file(fname,
                      pathlist=os.pathsep + subdir + os.pathsep,
                      cwd=False))

        # implicit extensions work (even if they are not necessary)
        self.assertEqual(os.path.join(self.tmpdir, fname),
                         find_file(fname, ext='.py'))
        self.assertEqual(os.path.join(self.tmpdir, fname),
                         find_file(fname, ext=['.py']))

        # implicit extensions work (and when they are not necessary)
        self.assertEqual(os.path.join(self.tmpdir, fname),
                         find_file(fname[:-3], ext='.py'))
        self.assertEqual(os.path.join(self.tmpdir, fname),
                         find_file(fname[:-3], ext=['.py']))

        # only files are found
        self.assertEqual(
            os.path.join(subdir, subdir_name),
            find_file(subdir_name, pathlist=[self.tmpdir, subdir], cwd=False))

        # empty dirs are skipped
        self.assertEqual(
            os.path.join(subdir, subdir_name),
            find_file(subdir_name,
                      pathlist=['', self.tmpdir, subdir],
                      cwd=False))
コード例 #2
0
ファイル: test_fileutils.py プロジェクト: Pyomo/pyomo
    def test_findfile(self):
        self.tmpdir = os.path.abspath(tempfile.mkdtemp())
        subdir_name = 'aaa'
        subdir = os.path.join(self.tmpdir, subdir_name)
        os.mkdir(subdir)
        os.chdir(self.tmpdir)

        fname = 'foo.py'
        self.assertEqual(
            None,
            find_file(fname)
        )

        open(os.path.join(self.tmpdir,fname),'w').close()
        open(os.path.join(subdir,fname),'w').close()
        open(os.path.join(subdir,'aaa'),'w').close()
        # we can find files in the CWD
        self.assertEqual(
            os.path.join(self.tmpdir,fname),
            find_file(fname)
        )
        # unless we don't look in the cwd
        self.assertEqual(
            None,
            find_file(fname, cwd=False)
        )
        # cwd overrides pathlist
        self.assertEqual(
            os.path.join(self.tmpdir,fname),
            find_file(fname, pathlist=[subdir])
        )
        self.assertEqual(
            os.path.join(subdir,fname),
            find_file(fname, pathlist=[subdir], cwd=False)
        )
        # ...unless the CWD match fails the MODE check
        #  (except on Windows, where all files have X_OK)
        self.assertEqual(
            ( os.path.join(self.tmpdir,fname)
              if _system() in ('windows','cygwin')
              else None ),
            find_file(fname, pathlist=[subdir], mode=os.X_OK)
        )
        self._make_exec(os.path.join(subdir,fname))
        self.assertEqual(
            ( os.path.join(self.tmpdir,fname)
              if _system() in ('windows','cygwin')
              else os.path.join(subdir,fname) ),
            find_file(fname, pathlist=[subdir], mode=os.X_OK)
        )
        # pathlist may also be a string
        self.assertEqual(
            os.path.join(subdir,fname),
            find_file(fname, pathlist=os.pathsep+subdir+os.pathsep, cwd=False)
        )

        # implicit extensions work (even if they are not necessary)
        self.assertEqual(
            os.path.join(self.tmpdir,fname),
            find_file(fname, ext='.py')
        )
        self.assertEqual(
            os.path.join(self.tmpdir,fname),
            find_file(fname, ext=['.py'])
        )

        # implicit extensions work (and when they are not necessary)
        self.assertEqual(
            os.path.join(self.tmpdir,fname),
            find_file(fname[:-3], ext='.py')
        )
        self.assertEqual(
            os.path.join(self.tmpdir,fname),
            find_file(fname[:-3], ext=['.py'])
        )

        # only files are found
        self.assertEqual(
            os.path.join(subdir,subdir_name),
            find_file( subdir_name,
                       pathlist=[self.tmpdir, subdir], cwd=False )
        )

        # empty dirs are skipped
        self.assertEqual(
            os.path.join(subdir,subdir_name),
            find_file( subdir_name,
                       pathlist=['', self.tmpdir, subdir], cwd=False )
        )