Home | History | Annotate | Download | only in mac
      1 #!/usr/bin/env python
      2 
      3 # Copyright (c) 2012 Google Inc. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 """
      8 Verifies that app bundles are built correctly.
      9 """
     10 
     11 import TestGyp
     12 
     13 import os
     14 import plistlib
     15 import subprocess
     16 import sys
     17 
     18 def GetStdout(cmdlist):
     19   return subprocess.Popen(cmdlist,
     20                           stdout=subprocess.PIPE).communicate()[0].rstrip('\n')
     21 
     22 def ExpectEq(expected, actual):
     23   if expected != actual:
     24     print >>sys.stderr, 'Expected "%s", got "%s"' % (expected, actual)
     25     test.fail_test()
     26 
     27 def ls(path):
     28   '''Returns a list of all files in a directory, relative to the directory.'''
     29   result = []
     30   for dirpath, _, files in os.walk(path):
     31     for f in files:
     32       result.append(os.path.join(dirpath, f)[len(path) + 1:])
     33   return result
     34 
     35 def XcodeVersion():
     36   stdout = subprocess.check_output(['xcodebuild', '-version'])
     37   version = stdout.splitlines()[0].split()[-1].replace('.', '')
     38   return (version + '0' * (3 - len(version))).zfill(4)
     39 
     40 
     41 if sys.platform == 'darwin':
     42   test = TestGyp.TestGyp(formats=['ninja', 'make', 'xcode'])
     43 
     44   test.run_gyp('test.gyp', chdir='app-bundle')
     45 
     46   test.build('test.gyp', test.ALL, chdir='app-bundle')
     47 
     48   # Binary
     49   test.built_file_must_exist('Test App Gyp.app/Contents/MacOS/Test App Gyp',
     50                              chdir='app-bundle')
     51 
     52   # Info.plist
     53   info_plist = test.built_file_path('Test App Gyp.app/Contents/Info.plist',
     54                                     chdir='app-bundle')
     55   test.must_exist(info_plist)
     56   test.must_contain(info_plist, 'com.google.Test-App-Gyp')  # Variable expansion
     57   test.must_not_contain(info_plist, '${MACOSX_DEPLOYMENT_TARGET}');
     58 
     59   if test.format != 'make':
     60     # TODO: Synthesized plist entries aren't hooked up in the make generator.
     61     plist = plistlib.readPlist(info_plist)
     62     ExpectEq(GetStdout(['sw_vers', '-buildVersion']),
     63              plist['BuildMachineOSBuild'])
     64 
     65     # Prior to Xcode 5.0.0, SDKROOT (and thus DTSDKName) was only defined if
     66     # set in the Xcode project file. Starting with that version, it is always
     67     # defined.
     68     expected = ''
     69     if XcodeVersion() >= '0500':
     70       version = GetStdout(['xcodebuild', '-version', '-sdk', '', 'SDKVersion'])
     71       expected = 'macosx' + version
     72     ExpectEq(expected, plist['DTSDKName'])
     73     sdkbuild = GetStdout(
     74         ['xcodebuild', '-version', '-sdk', '', 'ProductBuildVersion'])
     75     if not sdkbuild:
     76       # Above command doesn't work in Xcode 4.2.
     77       sdkbuild = plist['BuildMachineOSBuild']
     78     ExpectEq(sdkbuild, plist['DTSDKBuild'])
     79     xcode, build = GetStdout(['xcodebuild', '-version']).splitlines()
     80     xcode = xcode.split()[-1].replace('.', '')
     81     xcode = (xcode + '0' * (3 - len(xcode))).zfill(4)
     82     build = build.split()[-1]
     83     ExpectEq(xcode, plist['DTXcode'])
     84     ExpectEq(build, plist['DTXcodeBuild'])
     85 
     86   # Resources
     87   strings_files = ['InfoPlist.strings', 'utf-16be.strings', 'utf-16le.strings']
     88   for f in strings_files:
     89     strings = test.built_file_path(
     90         os.path.join('Test App Gyp.app/Contents/Resources/English.lproj', f),
     91         chdir='app-bundle')
     92     test.must_exist(strings)
     93     # Xcodes writes UTF-16LE with BOM.
     94     contents = open(strings, 'rb').read()
     95     if not contents.startswith('\xff\xfe' + '/* Localized'.encode('utf-16le')):
     96       test.fail_test()
     97 
     98   test.built_file_must_exist(
     99       'Test App Gyp.app/Contents/Resources/English.lproj/MainMenu.nib',
    100       chdir='app-bundle')
    101 
    102   # Packaging
    103   test.built_file_must_exist('Test App Gyp.app/Contents/PkgInfo',
    104                              chdir='app-bundle')
    105   test.built_file_must_match('Test App Gyp.app/Contents/PkgInfo', 'APPLause',
    106                              chdir='app-bundle')
    107 
    108   # Check that no other files get added to the bundle.
    109   if set(ls(test.built_file_path('Test App Gyp.app', chdir='app-bundle'))) != \
    110      set(['Contents/MacOS/Test App Gyp',
    111           'Contents/Info.plist',
    112           'Contents/Resources/English.lproj/MainMenu.nib',
    113           'Contents/PkgInfo',
    114           ] +
    115          [os.path.join('Contents/Resources/English.lproj', f)
    116              for f in strings_files]):
    117     test.fail_test()
    118 
    119   test.pass_test()
    120