Add tests for makefile_writer.py.
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 26 Mar 2014 19:46:51 +0000 (19:46 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 26 Mar 2014 19:46:51 +0000 (19:46 +0000)
Many of the tests performed are comparison tests, where we call the
functions using dummy values and then compare to expectations (also
newly added).

Add a way to rebaseline the test for write_android_mk, which is likely
to change in the future.

BUG=skia:1975
R=epoger@google.com

Author: scroggo@google.com

Review URL: https://codereview.chromium.org/205383006

git-svn-id: http://skia.googlecode.com/svn/trunk@13957 2bbb7eff-a529-9590-31e7-b0007b416f81

platform_tools/android/gyp_gen/makefile_writer.py
platform_tools/android/tests/expectations/Android.mk [new file with mode: 0644]
platform_tools/android/tests/expectations/animals.txt [new file with mode: 0644]
platform_tools/android/tests/expectations/animals_append.txt [new file with mode: 0644]
platform_tools/android/tests/expectations/write_local_vars_append_arm [new file with mode: 0644]
platform_tools/android/tests/expectations/write_local_vars_append_foo [new file with mode: 0644]
platform_tools/android/tests/expectations/write_local_vars_append_no_name [new file with mode: 0644]
platform_tools/android/tests/expectations/write_local_vars_no_append_arm [new file with mode: 0644]
platform_tools/android/tests/expectations/write_local_vars_no_append_foo [new file with mode: 0644]
platform_tools/android/tests/expectations/write_local_vars_no_append_no_name [new file with mode: 0644]
platform_tools/android/tests/makefile_writer_tests.py [new file with mode: 0644]

index 791d225f8b89328c5d6a298f069c8149317fe25e..ea767f6958de95223d59732a0437cb877e702bd2 100644 (file)
@@ -11,7 +11,6 @@ Functions for creating an Android.mk from already created dictionaries.
 
 import os
 
-# TODO(scroggo): Add unit tests for this file.
 def write_group(f, name, items, append):
   """
   Helper function to list all names passed to a variable.
diff --git a/platform_tools/android/tests/expectations/Android.mk b/platform_tools/android/tests/expectations/Android.mk
new file mode 100644 (file)
index 0000000..6b5ff8b
--- /dev/null
@@ -0,0 +1,136 @@
+
+###############################################################################
+#
+# THIS FILE IS AUTOGENERATED BY GYP_TO_ANDROID.PY. DO NOT EDIT.
+#
+###############################################################################
+
+BASE_PATH := $(call my-dir)
+LOCAL_PATH:= $(call my-dir)
+
+###############################################################################
+#
+# PROBLEMS WITH SKIA DEBUGGING?? READ THIS...
+#
+# The debug build results in changes to the Skia headers. This means that those
+# using libskia must also be built with the debug version of the Skia headers.
+# There are a few scenarios where this comes into play:
+#
+# (1) You're building debug code that depends on libskia.
+#   (a) If libskia is built in release, then define SK_RELEASE when building
+#       your sources.
+#   (b) If libskia is built with debugging (see step 2), then no changes are
+#       needed since your sources and libskia have been built with SK_DEBUG.
+# (2) You're building libskia in debug mode.
+#   (a) RECOMMENDED: You can build the entire system in debug mode. Do this by
+#       updating your build/config.mk to include -DSK_DEBUG on the line that
+#       defines COMMON_GLOBAL_CFLAGS
+#   (b) You can update all the users of libskia to define SK_DEBUG when they are
+#       building their sources.
+#
+# NOTE: If neither SK_DEBUG or SK_RELEASE are defined then Skia checks NDEBUG to
+#       determine which build type to use.
+###############################################################################
+
+include $(CLEAR_VARS)
+LOCAL_ARM_MODE := thumb
+ifeq ($(TARGET_ARCH),arm)
+       ifeq ($(ARCH_ARM_HAVE_VFP),true)
+               LOCAL_CFLAGS += -DANDROID_LARGE_MEMORY_DEVICE
+       endif
+else
+       LOCAL_CFLAGS += -DANDROID_LARGE_MEMORY_DEVICE
+endif
+
+# used for testing
+#LOCAL_CFLAGS += -g -O0
+
+ifeq ($(NO_FALLBACK_FONT),true)
+       LOCAL_CFLAGS += -DNO_FALLBACK_FONT
+endif
+
+LOCAL_CFLAGS += \
+       local_cflags
+
+LOCAL_CPPFLAGS := \
+       local_cppflags
+
+LOCAL_SRC_FILES := \
+       local_src_files
+
+LOCAL_SHARED_LIBRARIES := \
+       local_shared_libraries
+
+LOCAL_STATIC_LIBRARIES := \
+       local_static_libraries
+
+LOCAL_C_INCLUDES := \
+       local_c_includes
+
+LOCAL_EXPORT_C_INCLUDE_DIRS := \
+       local_export_c_include_dirs
+
+ifeq ($(COND), true)
+LOCAL_CFLAGS_foo += \
+       local_cflags_foo
+
+LOCAL_CPPFLAGS_foo += \
+       local_cppflags_foo
+
+LOCAL_SRC_FILES_foo += \
+       local_src_files_foo
+
+LOCAL_SHARED_LIBRARIES_foo += \
+       local_shared_libraries_foo
+
+LOCAL_STATIC_LIBRARIES_foo += \
+       local_static_libraries_foo
+
+LOCAL_C_INCLUDES_foo += \
+       local_c_includes_foo
+
+LOCAL_EXPORT_C_INCLUDE_DIRS_foo += \
+       local_export_c_include_dirs_foo
+
+endif
+
+LOCAL_CFLAGS_bar += \
+       local_cflags_bar
+
+LOCAL_CPPFLAGS_bar += \
+       local_cppflags_bar
+
+LOCAL_SRC_FILES_bar += \
+       local_src_files_bar
+
+LOCAL_SHARED_LIBRARIES_bar += \
+       local_shared_libraries_bar
+
+LOCAL_STATIC_LIBRARIES_bar += \
+       local_static_libraries_bar
+
+LOCAL_C_INCLUDES_bar += \
+       local_c_includes_bar
+
+LOCAL_EXPORT_C_INCLUDE_DIRS_bar += \
+       local_export_c_include_dirs_bar
+
+include external/stlport/libstlport.mk
+LOCAL_MODULE:= libskia
+include $(BUILD_SHARED_LIBRARY)
+
+#############################################################
+# Build the skia tools
+#
+
+# benchmark (timings)
+#include $(BASE_PATH)/bench/Android.mk
+
+# golden-master (fidelity / regression test)
+#include $(BASE_PATH)/gm/Android.mk
+
+# unit-tests
+#include $(BASE_PATH)/tests/Android.mk
+
+# pathOps unit-tests
+# TODO include those sources!
diff --git a/platform_tools/android/tests/expectations/animals.txt b/platform_tools/android/tests/expectations/animals.txt
new file mode 100644 (file)
index 0000000..6685dd3
--- /dev/null
@@ -0,0 +1,6 @@
+animals := \
+       dog \
+       cat \
+       mouse \
+       elephant
+
diff --git a/platform_tools/android/tests/expectations/animals_append.txt b/platform_tools/android/tests/expectations/animals_append.txt
new file mode 100644 (file)
index 0000000..96b6b99
--- /dev/null
@@ -0,0 +1,6 @@
+animals_append += \
+       dog \
+       cat \
+       mouse \
+       elephant
+
diff --git a/platform_tools/android/tests/expectations/write_local_vars_append_arm b/platform_tools/android/tests/expectations/write_local_vars_append_arm
new file mode 100644 (file)
index 0000000..d9f5c88
--- /dev/null
@@ -0,0 +1,21 @@
+LOCAL_CFLAGS_arm += \
+       local_cflags
+
+LOCAL_CPPFLAGS_arm += \
+       local_cppflags
+
+LOCAL_SRC_FILES_arm += \
+       local_src_files
+
+LOCAL_SHARED_LIBRARIES_arm += \
+       local_shared_libraries
+
+LOCAL_STATIC_LIBRARIES_arm += \
+       local_static_libraries
+
+LOCAL_C_INCLUDES_arm += \
+       local_c_includes
+
+LOCAL_EXPORT_C_INCLUDE_DIRS_arm += \
+       local_export_c_include_dirs
+
diff --git a/platform_tools/android/tests/expectations/write_local_vars_append_foo b/platform_tools/android/tests/expectations/write_local_vars_append_foo
new file mode 100644 (file)
index 0000000..be9a814
--- /dev/null
@@ -0,0 +1,21 @@
+LOCAL_CFLAGS_foo += \
+       local_cflags
+
+LOCAL_CPPFLAGS_foo += \
+       local_cppflags
+
+LOCAL_SRC_FILES_foo += \
+       local_src_files
+
+LOCAL_SHARED_LIBRARIES_foo += \
+       local_shared_libraries
+
+LOCAL_STATIC_LIBRARIES_foo += \
+       local_static_libraries
+
+LOCAL_C_INCLUDES_foo += \
+       local_c_includes
+
+LOCAL_EXPORT_C_INCLUDE_DIRS_foo += \
+       local_export_c_include_dirs
+
diff --git a/platform_tools/android/tests/expectations/write_local_vars_append_no_name b/platform_tools/android/tests/expectations/write_local_vars_append_no_name
new file mode 100644 (file)
index 0000000..302a4fe
--- /dev/null
@@ -0,0 +1,21 @@
+LOCAL_CFLAGS += \
+       local_cflags
+
+LOCAL_CPPFLAGS += \
+       local_cppflags
+
+LOCAL_SRC_FILES += \
+       local_src_files
+
+LOCAL_SHARED_LIBRARIES += \
+       local_shared_libraries
+
+LOCAL_STATIC_LIBRARIES += \
+       local_static_libraries
+
+LOCAL_C_INCLUDES += \
+       local_c_includes
+
+LOCAL_EXPORT_C_INCLUDE_DIRS += \
+       local_export_c_include_dirs
+
diff --git a/platform_tools/android/tests/expectations/write_local_vars_no_append_arm b/platform_tools/android/tests/expectations/write_local_vars_no_append_arm
new file mode 100644 (file)
index 0000000..01aaecb
--- /dev/null
@@ -0,0 +1,21 @@
+LOCAL_CFLAGS_arm += \
+       local_cflags
+
+LOCAL_CPPFLAGS_arm := \
+       local_cppflags
+
+LOCAL_SRC_FILES_arm := \
+       local_src_files
+
+LOCAL_SHARED_LIBRARIES_arm := \
+       local_shared_libraries
+
+LOCAL_STATIC_LIBRARIES_arm := \
+       local_static_libraries
+
+LOCAL_C_INCLUDES_arm := \
+       local_c_includes
+
+LOCAL_EXPORT_C_INCLUDE_DIRS_arm := \
+       local_export_c_include_dirs
+
diff --git a/platform_tools/android/tests/expectations/write_local_vars_no_append_foo b/platform_tools/android/tests/expectations/write_local_vars_no_append_foo
new file mode 100644 (file)
index 0000000..22c8306
--- /dev/null
@@ -0,0 +1,21 @@
+LOCAL_CFLAGS_foo += \
+       local_cflags
+
+LOCAL_CPPFLAGS_foo := \
+       local_cppflags
+
+LOCAL_SRC_FILES_foo := \
+       local_src_files
+
+LOCAL_SHARED_LIBRARIES_foo := \
+       local_shared_libraries
+
+LOCAL_STATIC_LIBRARIES_foo := \
+       local_static_libraries
+
+LOCAL_C_INCLUDES_foo := \
+       local_c_includes
+
+LOCAL_EXPORT_C_INCLUDE_DIRS_foo := \
+       local_export_c_include_dirs
+
diff --git a/platform_tools/android/tests/expectations/write_local_vars_no_append_no_name b/platform_tools/android/tests/expectations/write_local_vars_no_append_no_name
new file mode 100644 (file)
index 0000000..6742050
--- /dev/null
@@ -0,0 +1,21 @@
+LOCAL_CFLAGS += \
+       local_cflags
+
+LOCAL_CPPFLAGS := \
+       local_cppflags
+
+LOCAL_SRC_FILES := \
+       local_src_files
+
+LOCAL_SHARED_LIBRARIES := \
+       local_shared_libraries
+
+LOCAL_STATIC_LIBRARIES := \
+       local_static_libraries
+
+LOCAL_C_INCLUDES := \
+       local_c_includes
+
+LOCAL_EXPORT_C_INCLUDE_DIRS := \
+       local_export_c_include_dirs
+
diff --git a/platform_tools/android/tests/makefile_writer_tests.py b/platform_tools/android/tests/makefile_writer_tests.py
new file mode 100644 (file)
index 0000000..9a764b5
--- /dev/null
@@ -0,0 +1,198 @@
+#!/usr/bin/python
+
+# Copyright 2014 Google Inc.
+#
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""
+Test makefile_writer.py
+"""
+
+import argparse
+import os
+import shutil
+import sys
+import tempfile
+import test_variables
+import unittest
+
+sys.path.append(test_variables.GYP_GEN_DIR)
+
+import makefile_writer
+import vars_dict_lib
+
+EXPECTATIONS_DIR = os.path.join(os.path.dirname(__file__), 'expectations')
+MAKEFILE_NAME = 'Android.mk'
+
+def generate_dummy_vars_dict(name):
+  """Create a VarsDict and fill it with dummy entries.
+
+  Args:
+      name: string to be appended to each entry, if not None.
+
+  Returns:
+      A VarsDict with dummy entries.
+  """
+  vars_dict = vars_dict_lib.VarsDict()
+  for key in vars_dict.keys():
+    entry = key.lower()
+    if name:
+      entry += '_' + name
+    vars_dict[key].add(entry)
+  return vars_dict
+
+
+def generate_dummy_vars_dict_data(name, condition):
+  """Create a dummy VarsDictData.
+
+  Create a dummy VarsDictData, using the name for both the contained
+  VarsDict and the VarsDictData
+
+  Args:
+      name: name used by both the returned VarsDictData and its contained
+          VarsDict.
+      condition: condition used by the returned VarsDictData.
+
+  Returns:
+      A VarsDictData with dummy values, using the passed in info.
+  """
+  vars_dict = generate_dummy_vars_dict(name)
+
+  return makefile_writer.VarsDictData(vars_dict=vars_dict, name=name,
+                                      condition=condition)
+
+
+def generate_dummy_makefile(target_dir):
+  """Create a dummy makefile to demonstrate how it works.
+
+  Use dummy values unrelated to any gyp files. Its output should remain the
+  same unless/until makefile_writer.write_android_mk changes.
+
+  Args:
+      target_dir: directory in which to write the resulting Android.mk
+  """
+  common_vars_dict = generate_dummy_vars_dict(None)
+
+  deviation_params = [('foo', 'COND'), ('bar', None)]
+  deviations = [generate_dummy_vars_dict_data(name, condition)
+                for (name, condition) in deviation_params]
+
+  makefile_writer.write_android_mk(target_dir=target_dir,
+                                   common=common_vars_dict,
+                                   deviations_from_common=deviations)
+
+
+class MakefileWriterTest(unittest.TestCase):
+
+  def test_write_group_empty(self):
+    f = tempfile.TemporaryFile()
+    assert f.tell() == 0
+    for empty in (None, []):
+      for truth in (True, False):
+        makefile_writer.write_group(f, 'name', empty, truth)
+        self.assertEqual(f.tell(), 0)
+    f.close()
+
+  def __compare_files(self, actual_name, expectation_name, msg=None):
+    """Check that two files are identical.
+
+    Assert line by line that the files match.
+
+    Args:
+        actual_name: Full path to the test file.
+        expectation_name: Basename of the expectations file within which
+            to compare. The file is expected to be in
+            platform_tools/android/tests/expectations.
+        msg: Message to pass to assertEqual.
+    Raises:
+        AssertionError: If the files do not match.
+    """
+    with open(actual_name, 'r') as result:
+      with open(os.path.join(EXPECTATIONS_DIR,
+                             expectation_name)) as expectation:
+        for line in result:
+          self.assertEqual(line, expectation.readline(), msg)
+
+  def test_write_group(self):
+    animals = ('dog', 'cat', 'mouse', 'elephant')
+    fd, filename = tempfile.mkstemp()
+    with open(filename, 'w') as f:
+      makefile_writer.write_group(f, 'animals', animals, False)
+    os.close(fd)
+    # Now confirm that it matches expectations
+    self.__compare_files(filename, 'animals.txt')
+
+    with open(filename, 'w') as f:
+      makefile_writer.write_group(f, 'animals_append', animals, True)
+    # Now confirm that it matches expectations
+    self.__compare_files(filename, 'animals_append.txt')
+    os.remove(filename)
+
+  def test_write_local_vars(self):
+    vars_dict = generate_dummy_vars_dict(None)
+
+    # Call variations of write_local_vars.
+    for append in [ True, False ]:
+      for name in [ None, 'arm', 'foo' ]:
+        # Now write to a temporary file.
+        fd, outfile = tempfile.mkstemp()
+        with open(outfile, 'w') as f:
+          makefile_writer.write_local_vars(f, vars_dict, append, name)
+        os.close(fd)
+
+        # Compare to the expected file.
+        filename = 'write_local_vars'
+        if append:
+          filename += '_append'
+        else:
+          filename += '_no_append'
+        if name:
+          filename += '_' + name
+        else:
+          filename += '_no_name'
+        self.__compare_files(outfile, filename)
+
+        # KNOWN_TARGETS is always a part of the input VarsDict, but it should
+        # not be written to the resulting file.
+        # Note that this assumes none of our dummy entries is 'KNOWN_TARGETS'.
+        known_targets_name = 'KNOWN_TARGETS'
+        self.assertEqual(len(vars_dict[known_targets_name]), 1)
+
+        with open(outfile, 'r') as f:
+          self.assertNotIn(known_targets_name, f.read())
+        os.remove(outfile)
+
+  def test_write_android_mk(self):
+    outdir = tempfile.mkdtemp()
+    generate_dummy_makefile(outdir)
+
+    self.__compare_files(os.path.join(outdir, MAKEFILE_NAME), MAKEFILE_NAME,
+                         'If you\'ve modified makefile_writer.py, run ' +
+                         '"makefile_writer_tests.py --rebaseline" ' +
+                         'to rebaseline')
+
+    shutil.rmtree(outdir)
+
+
+def main():
+  loader = unittest.TestLoader()
+  suite = loader.loadTestsFromTestCase(MakefileWriterTest)
+  results = unittest.TextTestRunner(verbosity=2).run(suite)
+  print repr(results)
+  if not results.wasSuccessful():
+    raise Exception('failed one or more unittests')
+
+
+if __name__ == '__main__':
+  parser = argparse.ArgumentParser()
+  parser.add_argument('-r', '--rebaseline',
+                      help='Rebaseline expectation for Android.mk',
+                      action='store_true')
+  args = parser.parse_args()
+
+  if args.rebaseline:
+    generate_dummy_makefile(EXPECTATIONS_DIR)
+  else:
+    main()
+