From 147087ca43f3a0c4713b85d3a6d175632474d370 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 24 Jan 2020 16:40:44 -0800 Subject: [PATCH 1/3] [test] Adjust new Substring test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We don’t have an easy way to check for the runtime version of the stdlib, so a two-way check for behavioral changes isn’t feasible. (Checking for the OS version isn’t good enough.) Only check for the new behavior, and only when we know for sure that it’s available. --- test/stdlib/subString.swift | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/test/stdlib/subString.swift b/test/stdlib/subString.swift index c1dd2291c2f07..9abdb7044066e 100644 --- a/test/stdlib/subString.swift +++ b/test/stdlib/subString.swift @@ -252,23 +252,21 @@ SubstringTests.test("UTF8View") { expectEqual("", String(u.dropFirst(100))!) expectEqual("", String(u.dropLast(100))!) - let expectSubstringWCSIA: Bool - // This availability guard should refer to a concrete OS version in - // future. - if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) { - expectSubstringWCSIA = true - } else { - expectSubstringWCSIA = false - } - checkHasContiguousStorage(s.utf8, expected: true) // Strings always do - checkHasContiguousStorage(t, expected: expectSubstringWCSIA) - checkHasContiguousStorage(u, expected: expectSubstringWCSIA) checkHasContiguousStorageSubstring(t) checkHasContiguousStorageSubstring(u) checkMatchContiguousStorage(Array(s.utf8), s.utf8, expected: true) - checkMatchContiguousStorage(Array(t), t, expected: expectSubstringWCSIA) - checkMatchContiguousStorage(Array(u), u, expected: expectSubstringWCSIA) + + // The specialization for Substring.withContiguousStorageIfAvailable was + // added in https://github.com/apple/swift/pull/29146. + guard #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) else { + return + } + + checkHasContiguousStorage(t, expected: true) + checkHasContiguousStorage(u, expected: true) + checkMatchContiguousStorage(Array(t), t, expected: true) + checkMatchContiguousStorage(Array(u), u, expected: true) } } From b7eb2fced0d498d3be3fd1ba8aeda8cb65d736af Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 24 Jan 2020 16:41:47 -0800 Subject: [PATCH 2/3] [test] Substring: remove unnecessary `expected` arguments The previous commit made these redundant. --- test/stdlib/subString.swift | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/test/stdlib/subString.swift b/test/stdlib/subString.swift index 9abdb7044066e..e670910bb55b9 100644 --- a/test/stdlib/subString.swift +++ b/test/stdlib/subString.swift @@ -12,22 +12,17 @@ func checkMatch(_ x: S, _ y: T, _ i: S.Index) expectEqual(x[i], y[i]) } -func checkMatchContiguousStorage(_ x: S, _ y: T, expected: Bool) +func checkMatchContiguousStorage(_ x: S, _ y: T) where S.Element == T.Element, S.Element: Equatable { let xElement = x.withContiguousStorageIfAvailable { $0.first } let yElement = y.withContiguousStorageIfAvailable { $0.first } - if expected { - expectEqual(xElement, yElement) - } else { - expectNotEqual(xElement, yElement) - } + expectEqual(xElement, yElement) } -func checkHasContiguousStorage(_ x: S, expected: Bool) { - let hasStorage = x.withContiguousStorageIfAvailable { _ in true } ?? false - expectEqual(hasStorage, expected) +func checkHasContiguousStorage(_ x: S) { + expectTrue(x.withContiguousStorageIfAvailable { _ in true } ?? false) } func checkHasContiguousStorageSubstring(_ x: Substring.UTF8View) { @@ -252,21 +247,21 @@ SubstringTests.test("UTF8View") { expectEqual("", String(u.dropFirst(100))!) expectEqual("", String(u.dropLast(100))!) - checkHasContiguousStorage(s.utf8, expected: true) // Strings always do + + checkHasContiguousStorage(s.utf8) // Strings always do checkHasContiguousStorageSubstring(t) checkHasContiguousStorageSubstring(u) - checkMatchContiguousStorage(Array(s.utf8), s.utf8, expected: true) + checkMatchContiguousStorage(Array(s.utf8), s.utf8) // The specialization for Substring.withContiguousStorageIfAvailable was // added in https://github.com/apple/swift/pull/29146. guard #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) else { return } - - checkHasContiguousStorage(t, expected: true) - checkHasContiguousStorage(u, expected: true) - checkMatchContiguousStorage(Array(t), t, expected: true) - checkMatchContiguousStorage(Array(u), u, expected: true) + checkHasContiguousStorage(t) + checkHasContiguousStorage(u) + checkMatchContiguousStorage(Array(t), t) + checkMatchContiguousStorage(Array(u), u) } } From ca4fc19a50571925e353d3ff3e80bc4a136c69e1 Mon Sep 17 00:00:00 2001 From: Ross Bayer Date: Sun, 26 Jan 2020 23:32:34 -0800 Subject: [PATCH 3/3] [Build System: build-script] Remove the tar module from swift_build_support. The single function it provided to archive a source diretory has been moved to build-script which was the only caller. --- utils/build-script | 24 ++++++- .../swift_build_support/tar.py | 34 --------- utils/swift_build_support/tests/test_tar.py | 72 ------------------- 3 files changed, 21 insertions(+), 109 deletions(-) delete mode 100644 utils/swift_build_support/swift_build_support/tar.py delete mode 100644 utils/swift_build_support/tests/test_tar.py diff --git a/utils/build-script b/utils/build-script index 436fb357bac59..e0bce39463355 100755 --- a/utils/build-script +++ b/utils/build-script @@ -30,7 +30,6 @@ from swift_build_support.swift_build_support import ( diagnostics, products, shell, - tar, targets, workspace ) @@ -89,6 +88,25 @@ def print_xcodebuild_versions(file=sys.stdout): file.flush() +def tar(source, destination): + """ + Create a gzip archive of the file at 'source' at the given + 'destination' path. + """ + # We do not use `tarfile` here because: + # - We wish to support LZMA2 compression while also supporting Python 2.7. + # - We wish to explicitly set the owner and group of the archive. + args = ['tar', '-c', '-z', '-f', destination] + + if platform.system() != 'Darwin' and platform.system() != 'Windows': + args += ['--owner=0', '--group=0'] + + # Discard stderr output such as 'tar: Failed to open ...'. We'll detect + # these cases using the exit code, which should cause 'shell.call' to + # raise. + shell.call(args + [source], stderr=shell.DEVNULL) + + class BuildScriptInvocation(object): """Represent a single build script invocation.""" @@ -1169,8 +1187,8 @@ def main_normal(): # run `tar` without the leading '/' (we remove it ourselves to keep # `tar` from emitting a warning). with shell.pushd(args.install_symroot): - tar.tar(source=prefix.lstrip('/'), - destination=args.symbols_package) + tar(source=prefix.lstrip('/'), + destination=args.symbols_package) return 0 diff --git a/utils/swift_build_support/swift_build_support/tar.py b/utils/swift_build_support/swift_build_support/tar.py deleted file mode 100644 index 15d29ee5d40b3..0000000000000 --- a/utils/swift_build_support/swift_build_support/tar.py +++ /dev/null @@ -1,34 +0,0 @@ -# swift_build_support/tar.py - Call tar from Python -*- python -*- -# -# This source file is part of the Swift.org open source project -# -# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -# Licensed under Apache License v2.0 with Runtime Library Exception -# -# See https://swift.org/LICENSE.txt for license information -# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors - -from __future__ import absolute_import - -import platform - -from . import shell - - -def tar(source, destination): - """ - Create a gzip archive of the file at 'source' at the given - 'destination' path. - """ - # We do not use `tarfile` here because: - # - We wish to support LZMA2 compression while also supporting Python 2.7. - # - We wish to explicitly set the owner and group of the archive. - args = ['tar', '-c', '-z', '-f', destination] - - if platform.system() != 'Darwin' and platform.system() != 'Windows': - args += ['--owner=0', '--group=0'] - - # Discard stderr output such as 'tar: Failed to open ...'. We'll detect - # these cases using the exit code, which should cause 'shell.call' to - # raise. - shell.call(args + [source], stderr=shell.DEVNULL) diff --git a/utils/swift_build_support/tests/test_tar.py b/utils/swift_build_support/tests/test_tar.py deleted file mode 100644 index 290a4179e4c78..0000000000000 --- a/utils/swift_build_support/tests/test_tar.py +++ /dev/null @@ -1,72 +0,0 @@ -# test_tar.py - Unit tests for swift_build_support.tar -*- python -*- -# -# This source file is part of the Swift.org open source project -# -# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors -# Licensed under Apache License v2.0 with Runtime Library Exception -# -# See https://swift.org/LICENSE.txt for license information -# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors - -import os -import platform -import sys -import tempfile -import unittest -try: - from StringIO import StringIO -except ImportError: - from io import StringIO - -from swift_build_support.tar import tar - - -class TarTestCase(unittest.TestCase): - def setUp(self): - self._orig_stdout = sys.stdout - self._orig_stderr = sys.stderr - self.stdout = StringIO() - self.stderr = StringIO() - sys.stdout = self.stdout - sys.stderr = self.stderr - - def tearDown(self): - sys.stdout = self._orig_stdout - sys.stderr = self._orig_stderr - - def test_tar_this_file_succeeds(self): - # `tar` complains about absolute paths, so use a relative path here. - if platform.system() != 'Windows': - source = os.path.relpath(__file__) - else: - # Windows can use absolute paths, specially because the relative - # path might not exist because the file and the current directory - # might be in different drives. - source = __file__ - _, destination = tempfile.mkstemp() - tar(source=source, destination=destination) - - if platform.system() == "Darwin" or platform.system() == 'Windows': - expect = "+ tar -c -z -f {dest} {source}\n" - else: - expect = "+ tar -c -z -f {dest} --owner=0 --group=0 {source}\n" - - self.assertEqual(self.stdout.getvalue(), "") - self.assertEqual(self.stderr.getvalue(), - expect.format(dest=self._platform_quote(destination), - source=self._platform_quote(source))) - - def test_tar_nonexistent_file_raises(self): - with self.assertRaises(SystemExit): - tar(source='/path/to/something/that/surely/doesnt/exist', - destination='/another/path/that/shouldnt/exist') - - def _platform_quote(self, path): - if platform.system() == 'Windows': - return "'{}'".format(path) - else: - return path - - -if __name__ == '__main__': - unittest.main()