From 5dfcef900e964af32bbbabd0a95d08d7a8d83d56 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 17 May 2018 12:04:13 +0100 Subject: [PATCH] Add linux test generation script --- Tests/LinuxMain.swift | 11 +- Tests/NIOHTTP2Tests/BasicTests+XCTest.swift | 6 +- .../HTTP2StreamMultiplexerTests+XCTest.swift | 54 ++++ .../ReentrancyTests+XCTest.swift | 35 +++ .../SimpleClientServerTests+XCTest.swift | 61 +++++ scripts/generate_linux_tests.rb | 231 ++++++++++++++++++ 6 files changed, 391 insertions(+), 7 deletions(-) create mode 100644 Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests+XCTest.swift create mode 100644 Tests/NIOHTTP2Tests/ReentrancyTests+XCTest.swift create mode 100644 Tests/NIOHTTP2Tests/SimpleClientServerTests+XCTest.swift create mode 100755 scripts/generate_linux_tests.rb diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 649a0c6..460177b 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -11,9 +11,9 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -/// -/// LinuxMain.swift -/// +// +// LinuxMain.swift +// import XCTest /// @@ -27,5 +27,8 @@ import XCTest XCTMain([ testCase(BasicTests.allTests), + testCase(HTTP2StreamMultiplexerTests.allTests), + testCase(ReentrancyTests.allTests), + testCase(SimpleClientServerTests.allTests), ]) -#endif \ No newline at end of file +#endif diff --git a/Tests/NIOHTTP2Tests/BasicTests+XCTest.swift b/Tests/NIOHTTP2Tests/BasicTests+XCTest.swift index a33aa7e..150c505 100644 --- a/Tests/NIOHTTP2Tests/BasicTests+XCTest.swift +++ b/Tests/NIOHTTP2Tests/BasicTests+XCTest.swift @@ -11,9 +11,9 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// -/// -/// BasicTests+XCTest.swift -/// +// +// BasicTests+XCTest.swift +// import XCTest /// diff --git a/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests+XCTest.swift b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests+XCTest.swift new file mode 100644 index 0000000..4d65810 --- /dev/null +++ b/Tests/NIOHTTP2Tests/HTTP2StreamMultiplexerTests+XCTest.swift @@ -0,0 +1,54 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// +// +// HTTP2StreamMultiplexerTests+XCTest.swift +// +import XCTest + +/// +/// NOTE: This file was generated by generate_linux_tests.rb +/// +/// Do NOT edit this file directly as it will be regenerated automatically when needed. +/// + +extension HTTP2StreamMultiplexerTests { + + static var allTests : [(String, (HTTP2StreamMultiplexerTests) -> () throws -> Void)] { + return [ + ("testMultiplexerIgnoresFramesOnStream0", testMultiplexerIgnoresFramesOnStream0), + ("testHeadersFramesCreateNewChannels", testHeadersFramesCreateNewChannels), + ("testChannelsCloseThemselvesWhenToldTo", testChannelsCloseThemselvesWhenToldTo), + ("testChannelsCloseAfterResetStreamFrameFirstThenEvent", testChannelsCloseAfterResetStreamFrameFirstThenEvent), + ("testChannelsCloseAfterGoawayFrameFirstThenEvent", testChannelsCloseAfterGoawayFrameFirstThenEvent), + ("testFramesForUnknownStreamsAreReported", testFramesForUnknownStreamsAreReported), + ("testFramesForClosedStreamsAreReported", testFramesForClosedStreamsAreReported), + ("testClosingIdleChannels", testClosingIdleChannels), + ("testClosingActiveChannels", testClosingActiveChannels), + ("testClosePromiseIsSatisfiedWithTheEvent", testClosePromiseIsSatisfiedWithTheEvent), + ("testMultipleClosePromisesAreSatisfied", testMultipleClosePromisesAreSatisfied), + ("testClosePromiseFailsWithError", testClosePromiseFailsWithError), + ("testFramesAreNotDeliveredUntilStreamIsSetUp", testFramesAreNotDeliveredUntilStreamIsSetUp), + ("testFramesAreNotDeliveredIfSetUpFails", testFramesAreNotDeliveredIfSetUpFails), + ("testFlushingOneChannelDoesntFlushThemAll", testFlushingOneChannelDoesntFlushThemAll), + ("testUnflushedWritesFailOnClose", testUnflushedWritesFailOnClose), + ("testUnflushedWritesFailOnError", testUnflushedWritesFailOnError), + ("testWritesFailOnClosedStreamChannels", testWritesFailOnClosedStreamChannels), + ("testReadPullsInAllFrames", testReadPullsInAllFrames), + ("testReadIsPerChannel", testReadIsPerChannel), + ("testReadWillCauseAutomaticFrameDelivery", testReadWillCauseAutomaticFrameDelivery), + ("testReadWithNoPendingDataCausesReadOnParentChannel", testReadWithNoPendingDataCausesReadOnParentChannel), + ] + } +} + diff --git a/Tests/NIOHTTP2Tests/ReentrancyTests+XCTest.swift b/Tests/NIOHTTP2Tests/ReentrancyTests+XCTest.swift new file mode 100644 index 0000000..fe33480 --- /dev/null +++ b/Tests/NIOHTTP2Tests/ReentrancyTests+XCTest.swift @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// +// +// ReentrancyTests+XCTest.swift +// +import XCTest + +/// +/// NOTE: This file was generated by generate_linux_tests.rb +/// +/// Do NOT edit this file directly as it will be regenerated automatically when needed. +/// + +extension ReentrancyTests { + + static var allTests : [(String, (ReentrancyTests) -> () throws -> Void)] { + return [ + ("testReEnterReadOnRead", testReEnterReadOnRead), + ("testReenterInactiveOnRead", testReenterInactiveOnRead), + ("testReenterReadEOFOnRead", testReenterReadEOFOnRead), + ] + } +} + diff --git a/Tests/NIOHTTP2Tests/SimpleClientServerTests+XCTest.swift b/Tests/NIOHTTP2Tests/SimpleClientServerTests+XCTest.swift new file mode 100644 index 0000000..3adfb2c --- /dev/null +++ b/Tests/NIOHTTP2Tests/SimpleClientServerTests+XCTest.swift @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// +// +// SimpleClientServerTests+XCTest.swift +// +import XCTest + +/// +/// NOTE: This file was generated by generate_linux_tests.rb +/// +/// Do NOT edit this file directly as it will be regenerated automatically when needed. +/// + +extension SimpleClientServerTests { + + static var allTests : [(String, (SimpleClientServerTests) -> () throws -> Void)] { + return [ + ("testBasicRequestResponse", testBasicRequestResponse), + ("testManyRequestsAtOnce", testManyRequestsAtOnce), + ("testNothingButGoaway", testNothingButGoaway), + ("testGoAwayWithStreamsUpQuiescing", testGoAwayWithStreamsUpQuiescing), + ("testLargeDataFramesAreSplit", testLargeDataFramesAreSplit), + ("testSendingDataFrameWithSmallFile", testSendingDataFrameWithSmallFile), + ("testSendingDataFrameWithLargeFile", testSendingDataFrameWithLargeFile), + ("testMoreRequestsThanMaxConcurrentStreamsAtOnce", testMoreRequestsThanMaxConcurrentStreamsAtOnce), + ("testOverridingDefaultSettings", testOverridingDefaultSettings), + ("testBasicPingFrames", testBasicPingFrames), + ("testUnflushedWritesAreFailedOnChannelInactive", testUnflushedWritesAreFailedOnChannelInactive), + ("testUnflushedWritesAreFailedOnHalfClosure", testUnflushedWritesAreFailedOnHalfClosure), + ("testUnflushedWritesAreFailedOnChannelInactiveRentrant", testUnflushedWritesAreFailedOnChannelInactiveRentrant), + ("testUnflushedWritesAreFailedOnHalfClosureReentrant", testUnflushedWritesAreFailedOnHalfClosureReentrant), + ("testFrameReceivesDoNotTriggerFlushes", testFrameReceivesDoNotTriggerFlushes), + ("testAutomaticFlowControl", testAutomaticFlowControl), + ("testPartialFrame", testPartialFrame), + ("testFailingUnflushedWritesForResetStream", testFailingUnflushedWritesForResetStream), + ("testFailingFlushedWritesForResetStream", testFailingFlushedWritesForResetStream), + ("testFailingUnflushedWritesForGoaway", testFailingUnflushedWritesForGoaway), + ("testFailingFlushedWritesForGoaway", testFailingFlushedWritesForGoaway), + ("testSendingTrailers", testSendingTrailers), + ("test1XXResponseHeaderFields", test1XXResponseHeaderFields), + ("testPriorityFramesAreNotEmitted", testPriorityFramesAreNotEmitted), + ("testStreamClosedWithNoError", testStreamClosedWithNoError), + ("testStreamClosedViaRstStream", testStreamClosedViaRstStream), + ("testStreamClosedViaGoaway", testStreamClosedViaGoaway), + ("testStreamCloseEventForRstStreamFiresAfterFrame", testStreamCloseEventForRstStreamFiresAfterFrame), + ("testStreamCloseEventForGoawayFiresAfterFrame", testStreamCloseEventForGoawayFiresAfterFrame), + ] + } +} + diff --git a/scripts/generate_linux_tests.rb b/scripts/generate_linux_tests.rb new file mode 100755 index 0000000..f2af9fd --- /dev/null +++ b/scripts/generate_linux_tests.rb @@ -0,0 +1,231 @@ +#!/usr/bin/env ruby + +# +# process_test_files.rb +# +# Copyright 2016 Tony Stone +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Created by Tony Stone on 5/4/16. +# +require 'getoptlong' +require 'fileutils' +require 'pathname' + +include FileUtils + +# +# This ruby script will auto generate LinuxMain.swift and the +XCTest.swift extension files for Swift Package Manager on Linux platforms. +# +# See https://github.com/apple/swift-corelibs-xctest/blob/master/Documentation/Linux.md +# +def header(fileName) + string = <<-eos +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// +// +// +// +import XCTest + +/// +/// NOTE: This file was generated by generate_linux_tests.rb +/// +/// Do NOT edit this file directly as it will be regenerated automatically when needed. +/// + eos + + string + .sub('', File.basename(fileName)) + .sub('', Time.now.to_s) +end + +def createExtensionFile(fileName, classes) + extensionFile = fileName.sub! '.swift', '+XCTest.swift' + print 'Creating file: ' + extensionFile + "\n" + + File.open(extensionFile, 'w') do |file| + file.write header(extensionFile) + file.write "\n" + + for classArray in classes + file.write 'extension ' + classArray[0] + " {\n\n" + file.write ' static var allTests : [(String, (' + classArray[0] + ") -> () throws -> Void)] {\n" + file.write " return [\n" + + for funcName in classArray[1] + file.write ' ("' + funcName + '", ' + funcName + "),\n" + end + + file.write " ]\n" + file.write " }\n" + file.write "}\n\n" + end + end +end + +def createLinuxMain(testsDirectory, allTestSubDirectories, files) + fileName = testsDirectory + '/LinuxMain.swift' + print 'Creating file: ' + fileName + "\n" + + File.open(fileName, 'w') do |file| + file.write header(fileName) + file.write "\n" + + file.write "#if os(Linux) || os(FreeBSD)\n" + for testSubDirectory in allTestSubDirectories.sort { |x, y| x <=> y } + file.write ' @testable import ' + testSubDirectory + "\n" + end + file.write "\n" + file.write " XCTMain([\n" + + testCases = [] + for classes in files + for classArray in classes + testCases << classArray[0] + end + end + + for testCase in testCases.sort { |x, y| x <=> y } + file.write ' testCase(' + testCase + ".allTests),\n" + end + file.write " ])\n" + file.write "#endif\n" + end +end + +def parseSourceFile(fileName) + puts 'Parsing file: ' + fileName + "\n" + + classes = [] + currentClass = nil + inIfLinux = false + inElse = false + ignore = false + + # + # Read the file line by line + # and parse to find the class + # names and func names + # + File.readlines(fileName).each do |line| + if inIfLinux + if /\#else/.match(line) + inElse = true + ignore = true + else + if /\#end/.match(line) + inElse = false + inIfLinux = false + ignore = false + end + end + else + if /\#if[ \t]+os\(Linux\)/.match(line) + inIfLinux = true + ignore = false + end + end + + next if ignore + # Match class or func + match = line[/class[ \t]+[a-zA-Z0-9_]*(?=[ \t]*:[ \t]*XCTestCase)|func[ \t]+test[a-zA-Z0-9_]*(?=[ \t]*\(\))/, 0] + if match + + if match[/class/, 0] == 'class' + className = match.sub(/^class[ \t]+/, '') + # + # Create a new class / func structure + # and add it to the classes array. + # + currentClass = [className, []] + classes << currentClass + else # Must be a func + funcName = match.sub(/^func[ \t]+/, '') + # + # Add each func name the the class / func + # structure created above. + # + currentClass[1] << funcName + end + end + end + classes +end + +# +# Main routine +# +# + +testsDirectory = 'Tests' + +options = GetoptLong.new(['--tests-dir', GetoptLong::OPTIONAL_ARGUMENT]) +options.quiet = true + +begin + options.each do |option, value| + case option + when '--tests-dir' + testsDirectory = value + end + end +rescue GetoptLong::InvalidOption +end + +allTestSubDirectories = [] +allFiles = [] + +Dir[testsDirectory + '/*'].each do |subDirectory| + next unless File.directory?(subDirectory) + directoryHasClasses = false + Dir[subDirectory + '/*Test{s,}.swift'].each do |fileName| + next unless File.file? fileName + fileClasses = parseSourceFile(fileName) + + # + # If there are classes in the + # test source file, create an extension + # file for it. + # + next unless fileClasses.count > 0 + createExtensionFile(fileName, fileClasses) + directoryHasClasses = true + allFiles << fileClasses + end + + if directoryHasClasses + allTestSubDirectories << Pathname.new(subDirectory).split.last.to_s + end +end + +# +# Last step is the create a LinuxMain.swift file that +# references all the classes and funcs in the source files. +# +if allFiles.count > 0 + createLinuxMain(testsDirectory, allTestSubDirectories, allFiles) +end +# eof