[commit: packages/directory] improve-tests-for-real: Add test-related files to extra-source-files (4d58b3a)

git at git.haskell.org git at git.haskell.org
Thu Mar 19 11:37:48 UTC 2015


Repository : ssh://git@git.haskell.org/directory

On branch  : improve-tests-for-real
Link       : http://ghc.haskell.org/trac/ghc/changeset/4d58b3ad4e2b5afa6373e7adadc3adfa55e59d71/directory

>---------------------------------------------------------------

commit 4d58b3ad4e2b5afa6373e7adadc3adfa55e59d71
Author: Phil Ruffwind <rf at rufflewind.com>
Date:   Mon Mar 2 08:30:55 2015 -0500

    Add test-related files to extra-source-files


>---------------------------------------------------------------

4d58b3ad4e2b5afa6373e7adadc3adfa55e59d71
 directory.cabal                     |  18 ++++++
 testsuite/update-extra-source-files | 119 ++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+)

diff --git a/directory.cabal b/directory.cabal
index bb6f9b3..ad62a8b 100644
--- a/directory.cabal
+++ b/directory.cabal
@@ -29,6 +29,24 @@ extra-source-files:
     directory.buildinfo
     include/HsDirectoryConfig.h.in
     install-sh
+    tests/*.hs
+    tests/*.stderr
+    tests/*.stdout
+    tests/Makefile
+    tests/T4113.stdout-mingw32
+    tests/all.T
+    tests/copyFile001dir/source
+    tests/copyFile002dir/source
+    tests/createDirectory001.stdout-mingw32
+    tests/createDirectoryIfMissing001.stdout-mingw32
+    tests/getDirContents002.stderr-mingw32
+    tests/getPermissions001.stdout-alpha-dec-osf3
+    tests/getPermissions001.stdout-i386-unknown-freebsd
+    tests/getPermissions001.stdout-i386-unknown-openbsd
+    tests/getPermissions001.stdout-mingw
+    tests/getPermissions001.stdout-x86_64-unknown-openbsd
+    testsuite/ghc.patch
+    testsuite/run
 
 source-repository head
     type:     git
diff --git a/testsuite/update-extra-source-files b/testsuite/update-extra-source-files
new file mode 100755
index 0000000..4711734
--- /dev/null
+++ b/testsuite/update-extra-source-files
@@ -0,0 +1,119 @@
+#!/usr/bin/env python
+# since cabal is rather picky about wildcards in 'extra-source-files',
+# we have to fill this in ourselves; this script automates that
+import os, re, subprocess
+
+ensure_str_encoding = []
+def ensure_str(string):
+    '''Make sure that the argument is in fact a Unicode string.  If the
+    argument is not, then:
+
+      - on Python 2, it will be decoded using the preferred encoding;
+      - on Python 3, it will cause a `TypeError`.
+    '''
+    if getattr(str, "decode") and getattr(str, "encode"):
+        if isinstance(string, unicode):
+            return string
+        if not ensure_str_encoding:
+            import locale
+            ensure_str_encoding[0] = locale.getpreferredencoding(False)
+        return string.decode(ensure_str_encoding[0])
+    if isinstance(string, str):
+        return string
+    raise TypeError("not an instance of 'str': " + repr(string))
+
+def rename(src_filename, dest_filename):
+    '''Rename a file (allows overwrites on Windows).'''
+    import os
+    if os.name == "nt":
+        import ctypes
+        success = ctypes.windll.kernel32.MoveFileExW(
+            ensure_str(src_filename),
+            ensure_str(dest_filename),
+            ctypes.c_ulong(0x1))
+        if not success:
+            raise ctypes.WinError()
+    else:
+        os.rename(src_filename, dest_filename)
+
+def read_file(filename, binary=False):
+    '''Read the contents of a file.'''
+    with open(filename, "rb" if binary else "rt") as file:
+        return file.read()
+
+def write_file(filename, contents, binary=False):
+    '''Write the contents to a file as atomically as possible.'''
+    from tempfile import NamedTemporaryFile
+    def cleanup():
+        try:
+            os.remove(tmp_filename)
+        except Exception:
+            pass
+    try:
+        with NamedTemporaryFile(
+                mode="wb" if binary else "wt",
+                suffix=".tmp",
+                prefix=os.path.basename(filename) + ".",
+                dir=os.path.dirname(filename),
+                delete=False) as tmp_file:
+            tmp_file.write(contents)
+            tmp_filename = tmp_file.name
+    except:
+        cleanup()
+        raise
+    try:
+        rename(tmp_filename, filename)
+    except OSError:                     # only remove if the rename failed
+        cleanup()
+        raise
+
+def find_cabal_fn():
+    '''Obtain the filename of the `*.cabal` file.'''
+    fns = [fn for fn in os.listdir()
+           if re.match(r"[\w-]+.cabal", fn) and os.path.isfile(fn)]
+    if len(fns) < 1:
+        raise Exception("can't find .cabal file in current directory")
+    elif len(fns) > 1:
+        raise Exception("too many .cabal files in current directory")
+    return fns[0]
+
+def git_tracked_files():
+    '''Obtain the list of file tracked by Git.'''
+    return subprocess.check_output(
+        ["git", "ls-tree", "-r", "--name-only", "HEAD"]
+    ).decode("utf-8").split("\n")
+
+indent = " " * 4
+
+dir_name = "tests"
+
+# extensions that are always included by default
+src_extensions = [
+    ".hs",
+    ".stderr",
+    ".stdout",
+]
+
+# additional source files not covered by 'src_extensions'
+def check_fn(fn):
+    if os.path.basename(fn) == ".gitignore":
+        return
+    if not fn.startswith(dir_name + "/"):
+        return
+    if (os.path.dirname(fn) == dir_name and
+        os.path.splitext(fn)[1] in src_extensions):
+        return
+    return True
+srcs = [indent + fn + "\n" for fn in git_tracked_files() if check_fn(fn)]
+
+# convert the extensions into patterns
+src_patterns = [indent + dir_name + "/*" + ext + "\n"
+                for ext in src_extensions]
+
+# update the .cabal file
+cabal_fn = find_cabal_fn()
+contents = read_file(cabal_fn)
+write_file(cabal_fn,
+           re.sub(r"\n(\s*" + dir_name + "/\S*\n)+",
+                  "\n" + "".join(src_patterns + srcs),
+                  contents, count=1))



More information about the ghc-commits mailing list