gyptest-app.py 4.1 KB

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