From 0cbf4f565d0bd80a9ed8227c91697faa9ec18f71 Mon Sep 17 00:00:00 2001 From: Rahul Malik Date: Tue, 19 Mar 2019 22:26:09 -0700 Subject: [PATCH 1/3] Add linter for making sure linux main is kept up to date --- Tests/CoreTests/LinuxTestIndex.swift | 88 +++++++++++++------------- Tests/LinuxMain.swift | 11 ++-- Utility/GenerateTestCaseProvider.swift | 2 + Utility/lint.sh | 31 +++++++++ Utility/stable-output-test.sh | 2 +- 5 files changed, 83 insertions(+), 51 deletions(-) diff --git a/Tests/CoreTests/LinuxTestIndex.swift b/Tests/CoreTests/LinuxTestIndex.swift index f194e11f..14328e79 100644 --- a/Tests/CoreTests/LinuxTestIndex.swift +++ b/Tests/CoreTests/LinuxTestIndex.swift @@ -1,50 +1,48 @@ // @generated by GenerateTestCaseProvider.swift +// swiftformat:disable all import XCTest #if os(Linux) - // Generated Test Extension for StringExtensionsTests - extension StringExtensionsTests { - static var allTests = [ - ("testUppercaseFirst", testUppercaseFirst), - ("testLowercaseFirst", testLowercaseFirst), - ("testSuffixSubstring", testSuffixSubstring), - ] - } - - // Generated Test Extension for ObjectiveCIRTests - extension ObjectiveCIRTests { - static var allTests = [ - ("testDirtyPropertyOption", testDirtyPropertyOption), - ("testDirtyPropertyOptionMultiWord", testDirtyPropertyOptionMultiWord), - ("testEnumTypeName", testEnumTypeName), - ("testEnumToStringName", testEnumToStringName), - ("testEnumFromStringName", testEnumFromStringName), - ("testStatementSyntax", testStatementSyntax), - ("testMsgSyntax", testMsgSyntax), - ("testBlockSyntax", testBlockSyntax), - ("testScopeSyntax", testScopeSyntax), - ("testNestedScopeSyntax", testNestedScopeSyntax), - ("testIfStmt", testIfStmt), - ("testElseIfStmt", testElseIfStmt), - ("testElseStmt", testElseStmt), - ("testIfElseStmt", testIfElseStmt), - ] - } - - // Generated Test Extension for LanguagesTests - extension LanguagesTests { - static var allTests = [ - ("testSnakeCaseToCamelCase", testSnakeCaseToCamelCase), - ("testSnakeCaseToPropertyName", testSnakeCaseToPropertyName), - ("testSnakeCaseToCapitalizedPropertyName", testSnakeCaseToCapitalizedPropertyName), - ("testReservedKeywordSubstitution", testReservedKeywordSubstitution), - ] - } - - // Generated Test Extension for ObjectiveCInitTests - extension ObjectiveCInitTests { - static var allTests = [ - ("testOneOfInit", testOneOfInit), - ] - } +// Generated Test Extension for StringExtensionsTests +extension StringExtensionsTests { + static var allTests = [ + ("testUppercaseFirst", testUppercaseFirst), + ("testLowercaseFirst", testLowercaseFirst), + ("testSuffixSubstring", testSuffixSubstring) + ] +} +// Generated Test Extension for ObjectiveCIRTests +extension ObjectiveCIRTests { + static var allTests = [ + ("testDirtyPropertyOption", testDirtyPropertyOption), + ("testDirtyPropertyOptionMultiWord", testDirtyPropertyOptionMultiWord), + ("testEnumTypeName", testEnumTypeName), + ("testEnumToStringName", testEnumToStringName), + ("testEnumFromStringName", testEnumFromStringName), + ("testStatementSyntax", testStatementSyntax), + ("testMsgSyntax", testMsgSyntax), + ("testBlockSyntax", testBlockSyntax), + ("testScopeSyntax", testScopeSyntax), + ("testNestedScopeSyntax", testNestedScopeSyntax), + ("testIfStmt", testIfStmt), + ("testElseIfStmt", testElseIfStmt), + ("testElseStmt", testElseStmt), + ("testIfElseStmt", testIfElseStmt) + ] +} +// Generated Test Extension for LanguagesTests +extension LanguagesTests { + static var allTests = [ + ("testSnakeCaseToCamelCase", testSnakeCaseToCamelCase), + ("testSnakeCaseToPropertyName", testSnakeCaseToPropertyName), + ("testSnakeCaseToCapitalizedPropertyName", testSnakeCaseToCapitalizedPropertyName), + ("testReservedKeywordSubstitution", testReservedKeywordSubstitution) + ] +} +// Generated Test Extension for ObjectiveCInitTests +extension ObjectiveCInitTests { + static var allTests = [ + ("testOneOfInit", testOneOfInit) + ] +} #endif diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index e54be545..c6df7f59 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -1,11 +1,12 @@ -@testable import CoreTests // @generated - Generated by GeneratedTestCaseProvider.swift +// swiftformat:disable all import XCTest +@testable import CoreTests var tests = [XCTestCaseEntry]() -tests += [testCase(StringExtensionsTests.allTests)] -tests += [testCase(ObjectiveCIRTests.allTests)] -tests += [testCase(LanguagesTests.allTests)] -tests += [testCase(ObjectiveCInitTests.allTests)] + tests += [testCase(StringExtensionsTests.allTests)] + tests += [testCase(ObjectiveCIRTests.allTests)] + tests += [testCase(LanguagesTests.allTests)] + tests += [testCase(ObjectiveCInitTests.allTests)] XCTMain(tests) diff --git a/Utility/GenerateTestCaseProvider.swift b/Utility/GenerateTestCaseProvider.swift index 71e3fc09..17943d35 100755 --- a/Utility/GenerateTestCaseProvider.swift +++ b/Utility/GenerateTestCaseProvider.swift @@ -28,6 +28,7 @@ func printLinuxMain(withClassNames classNames: [String]) -> String { }.joined(separator: "\n") return [ "// @generated - Generated by GeneratedTestCaseProvider.swift", + "// swiftformat:disable all", "import XCTest", "@testable import CoreTests", "", @@ -96,6 +97,7 @@ func processDirectory(atPath path: String) { .joined(separator: "\n") let output = [ "// @generated by GenerateTestCaseProvider.swift", + "// swiftformat:disable all", "import XCTest", "", "#if os(Linux)", diff --git a/Utility/lint.sh b/Utility/lint.sh index ad10823b..441f0cff 100755 --- a/Utility/lint.sh +++ b/Utility/lint.sh @@ -2,8 +2,39 @@ set -eou pipefail +update_current_line() { + printf "\033[1A" # move cursor one line up + printf "\033[K" # delete till end of line + echo "$1" +} + +log_err() { + echo "❌ $1" +} + +log_success() { + update_current_line "✅ $1" +} + echo "Checking for lint errors with SwiftLint" swift run swiftlint lint --reporter emoji echo "Checking for formatting errors with SwiftFormat" swift run swiftformat --lint . + +echo "Checking if linux test index needs to be updated" +readonly TEMP_DIR=$(mktemp -d) +cp -R Tests "${TEMP_DIR}" +cp -R Utility "${TEMP_DIR}" +cp Makefile "${TEMP_DIR}" +(cd "${TEMP_DIR}" && make build_test_index_linux) + +# Check if files changed, fail +if ! diff -rq "${TEMP_DIR}/Tests" "Tests"; then + log_err "Linux test index is out-of-date. Please update with 'make build_test_index_linux' and commit the changes" + exit 1 +else + log_success "Linux test index is up-to-date!" +fi + + diff --git a/Utility/stable-output-test.sh b/Utility/stable-output-test.sh index 7dab0207..008482f5 100755 --- a/Utility/stable-output-test.sh +++ b/Utility/stable-output-test.sh @@ -21,7 +21,7 @@ for lang in ${lang_options[@]}; do update_current_line "[${lang}] Testing output is stable (test case ${i}/100)" treatment_lang_output_dir="$(mktemp -d)" $plank_bin --lang ${lang} --output_dir=${treatment_lang_output_dir} ${json_files} - if ! diff "${lang_output_dir}" "${treatment_lang_output_dir}"; then + if ! diff -rq "${lang_output_dir}" "${treatment_lang_output_dir}"; then update_current_line "[${lang}] ❌ Output is unstable" exit 1 fi From 31a67520cbfc152c22c65116427fcebab1c9acaf Mon Sep 17 00:00:00 2001 From: Rahul Malik Date: Wed, 20 Mar 2019 09:31:44 -0700 Subject: [PATCH 2/3] Print diff output in lint script and stable output testing script --- Utility/lint.sh | 5 ++--- Utility/stable-output-test.sh | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Utility/lint.sh b/Utility/lint.sh index 441f0cff..329f9fb3 100755 --- a/Utility/lint.sh +++ b/Utility/lint.sh @@ -27,10 +27,9 @@ readonly TEMP_DIR=$(mktemp -d) cp -R Tests "${TEMP_DIR}" cp -R Utility "${TEMP_DIR}" cp Makefile "${TEMP_DIR}" -(cd "${TEMP_DIR}" && make build_test_index_linux) - +(cd "${TEMP_DIR}" && make build_test_index_linux > /dev/null) # Check if files changed, fail -if ! diff -rq "${TEMP_DIR}/Tests" "Tests"; then +if ! diff -r "${TEMP_DIR}/Tests" "Tests"; then log_err "Linux test index is out-of-date. Please update with 'make build_test_index_linux' and commit the changes" exit 1 else diff --git a/Utility/stable-output-test.sh b/Utility/stable-output-test.sh index 008482f5..02f0a51c 100755 --- a/Utility/stable-output-test.sh +++ b/Utility/stable-output-test.sh @@ -21,7 +21,7 @@ for lang in ${lang_options[@]}; do update_current_line "[${lang}] Testing output is stable (test case ${i}/100)" treatment_lang_output_dir="$(mktemp -d)" $plank_bin --lang ${lang} --output_dir=${treatment_lang_output_dir} ${json_files} - if ! diff -rq "${lang_output_dir}" "${treatment_lang_output_dir}"; then + if ! diff -r "${lang_output_dir}" "${treatment_lang_output_dir}"; then update_current_line "[${lang}] ❌ Output is unstable" exit 1 fi From 15b1c594fcba744e6c550016407627ba49af9aea Mon Sep 17 00:00:00 2001 From: Rahul Malik Date: Wed, 20 Mar 2019 09:41:39 -0700 Subject: [PATCH 3/3] Sort the files we parse for generating the linux test index - This makes the result stable, caught on CI --- Tests/CoreTests/LinuxTestIndex.swift | 28 +++++++++++++------------- Tests/LinuxMain.swift | 4 ++-- Utility/GenerateTestCaseProvider.swift | 1 + 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Tests/CoreTests/LinuxTestIndex.swift b/Tests/CoreTests/LinuxTestIndex.swift index 14328e79..fd7f93a9 100644 --- a/Tests/CoreTests/LinuxTestIndex.swift +++ b/Tests/CoreTests/LinuxTestIndex.swift @@ -3,12 +3,13 @@ import XCTest #if os(Linux) -// Generated Test Extension for StringExtensionsTests -extension StringExtensionsTests { +// Generated Test Extension for LanguagesTests +extension LanguagesTests { static var allTests = [ - ("testUppercaseFirst", testUppercaseFirst), - ("testLowercaseFirst", testLowercaseFirst), - ("testSuffixSubstring", testSuffixSubstring) + ("testSnakeCaseToCamelCase", testSnakeCaseToCamelCase), + ("testSnakeCaseToPropertyName", testSnakeCaseToPropertyName), + ("testSnakeCaseToCapitalizedPropertyName", testSnakeCaseToCapitalizedPropertyName), + ("testReservedKeywordSubstitution", testReservedKeywordSubstitution) ] } // Generated Test Extension for ObjectiveCIRTests @@ -30,19 +31,18 @@ extension ObjectiveCIRTests { ("testIfElseStmt", testIfElseStmt) ] } -// Generated Test Extension for LanguagesTests -extension LanguagesTests { - static var allTests = [ - ("testSnakeCaseToCamelCase", testSnakeCaseToCamelCase), - ("testSnakeCaseToPropertyName", testSnakeCaseToPropertyName), - ("testSnakeCaseToCapitalizedPropertyName", testSnakeCaseToCapitalizedPropertyName), - ("testReservedKeywordSubstitution", testReservedKeywordSubstitution) - ] -} // Generated Test Extension for ObjectiveCInitTests extension ObjectiveCInitTests { static var allTests = [ ("testOneOfInit", testOneOfInit) ] } +// Generated Test Extension for StringExtensionsTests +extension StringExtensionsTests { + static var allTests = [ + ("testUppercaseFirst", testUppercaseFirst), + ("testLowercaseFirst", testLowercaseFirst), + ("testSuffixSubstring", testSuffixSubstring) + ] +} #endif diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index c6df7f59..18731c3a 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -4,9 +4,9 @@ import XCTest @testable import CoreTests var tests = [XCTestCaseEntry]() - tests += [testCase(StringExtensionsTests.allTests)] - tests += [testCase(ObjectiveCIRTests.allTests)] tests += [testCase(LanguagesTests.allTests)] + tests += [testCase(ObjectiveCIRTests.allTests)] tests += [testCase(ObjectiveCInitTests.allTests)] + tests += [testCase(StringExtensionsTests.allTests)] XCTMain(tests) diff --git a/Utility/GenerateTestCaseProvider.swift b/Utility/GenerateTestCaseProvider.swift index 17943d35..f1f40216 100755 --- a/Utility/GenerateTestCaseProvider.swift +++ b/Utility/GenerateTestCaseProvider.swift @@ -92,6 +92,7 @@ func processDirectory(atPath path: String) { guard path != "" else { return } if let files = try? FileManager.default.contentsOfDirectory(atPath: path) { let generatedOutput = files + .sorted() .map { processFile(withPath: path + "/" + $0) } .filter { $0 != "" } .joined(separator: "\n")