17841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com#!/usr/bin/env python
27841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com
37841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com# Copyright (c) 2014 Google Inc. All rights reserved.
47841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com# Use of this source code is governed by a BSD-style license that can be
57841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com# found in the LICENSE file.
67841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com
77841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com"""
87841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.comMake sure overwriting read-only files works as expected (via win-tool).
97841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com"""
107841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com
117841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.comimport TestGyp
127841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com
137841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.comimport filecmp
147841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.comimport os
157841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.comimport stat
167841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.comimport sys
177841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com
187841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.comif sys.platform == 'win32':
197841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  test = TestGyp.TestGyp(formats=['ninja'])
207841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com
217841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  # First, create the source files.
227841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  os.makedirs('subdir')
237841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  read_only_files = ['read-only-file', 'subdir/A', 'subdir/B', 'subdir/C']
247841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  for f in read_only_files:
257841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com    test.write(f, 'source_contents')
267841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com    test.chmod(f, stat.S_IREAD)
277841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com    if os.access(f, os.W_OK):
287841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com      test.fail_test()
297841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com
307841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  # Second, create the read-only destination files. Note that we are creating
317841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  # them where the ninja and win-tool will try to copy them to, in order to test
327841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  # that copies overwrite the files.
337841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  os.makedirs(test.built_file_path('dest/subdir'))
347841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  for f in read_only_files:
357841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com    f = os.path.join('dest', f)
367841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com    test.write(test.built_file_path(f), 'SHOULD BE OVERWRITTEN')
377841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com    test.chmod(test.built_file_path(f), stat.S_IREAD)
387841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com    # Ensure not writable.
397841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com    if os.access(test.built_file_path(f), os.W_OK):
407841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com      test.fail_test()
417841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com
427841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  test.run_gyp('copies_readonly_files.gyp')
437841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  test.build('copies_readonly_files.gyp')
447841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com
457841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  # Check the destination files were overwritten by ninja.
467841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  for f in read_only_files:
477841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com    f = os.path.join('dest', f)
487841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com    test.must_contain(test.built_file_path(f), 'source_contents')
497841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com
507841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  # This will fail if the files are not the same mode or contents.
517841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  for f in read_only_files:
527841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com    if not filecmp.cmp(f, test.built_file_path(os.path.join('dest', f))):
537841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com      test.fail_test()
547841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com
557841557a512d9b2a54ec1fe9e43218024a6dc29edimator@google.com  test.pass_test()
56