Skip to content

Commit

Permalink
test formatting issues as part of sanity check (#84)
Browse files Browse the repository at this point in the history
motivaiton: unified format

changes:
* use official docker image 🎉
* fix outstanding formatting issues
* add a call to swiftformat as part of sanity script
* fix sanity script language check debugging statements
  • Loading branch information
tomerd authored Jul 30, 2019
1 parent 99786c8 commit 2637350
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 89 deletions.
10 changes: 6 additions & 4 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# file options

--exclude .build

# format options

--self insert
--patternlet inline
--stripunusedargs unnamed-only
--self insert
--patternlet inline
--stripunusedargs unnamed-only
--comments ignore
--ifdef no-indent

# rules

4 changes: 2 additions & 2 deletions Sources/Logging/Locks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
//===----------------------------------------------------------------------===//

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import Darwin
import Darwin
#else
import Glibc
import Glibc
#endif

/// A threading lock based on `libpthread` instead of `libdispatch`.
Expand Down
13 changes: 6 additions & 7 deletions Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
//===----------------------------------------------------------------------===//

#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import Darwin
import Darwin
#else
import Glibc
import Glibc
#endif

/// A `Logger` is the central type in `SwiftLog`. Its central function is to emit log messages using one of the methods
Expand Down Expand Up @@ -537,7 +537,6 @@ let systemStdout = Glibc.stdout!
/// `StreamLogHandler` is a simple implementation of `LogHandler` for directing
/// `Logger` output to either `stderr` or `stdout` via the factory methods.
public struct StreamLogHandler: LogHandler {

/// Factory that makes a `StreamLogHandler` to directs its output to `stdout`
public static func standardOutput(label: String) -> StreamLogHandler {
return StreamLogHandler(label: label, stream: StdioOutputStream.stdout)
Expand All @@ -555,16 +554,16 @@ public struct StreamLogHandler: LogHandler {
private var prettyMetadata: String?
public var metadata = Logger.Metadata() {
didSet {
prettyMetadata = prettify(metadata)
self.prettyMetadata = self.prettify(self.metadata)
}
}

public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
get {
return metadata[metadataKey]
return self.metadata[metadataKey]
}
set {
metadata[metadataKey] = newValue
self.metadata[metadataKey] = newValue
}
}

Expand All @@ -582,7 +581,7 @@ public struct StreamLogHandler: LogHandler {
: self.prettify(self.metadata.merging(metadata!, uniquingKeysWith: { _, new in new }))

var stream = self.stream
stream.write("\(timestamp()) \(level):\(prettyMetadata.map { " \($0)" } ?? "") \(message)\n")
stream.write("\(self.timestamp()) \(level):\(prettyMetadata.map { " \($0)" } ?? "") \(message)\n")
}

private func prettify(_ metadata: Logger.Metadata) -> String? {
Expand Down
14 changes: 7 additions & 7 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import XCTest
///

#if os(Linux) || os(FreeBSD)
@testable import LoggingTests
@testable import LoggingTests

XCTMain([
testCase(GlobalLoggerTest.allTests),
testCase(LocalLoggerTest.allTests),
testCase(LoggingTest.allTests),
testCase(MDCTest.allTests),
])
XCTMain([
testCase(GlobalLoggerTest.allTests),
testCase(LocalLoggerTest.allTests),
testCase(LoggingTest.allTests),
testCase(MDCTest.allTests),
])
#endif
6 changes: 3 additions & 3 deletions Tests/LoggingTests/GlobalLoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private struct Struct3 {
let group = DispatchGroup()
group.enter()
let loggingMetadata = MDC.global.metadata
queue.async {
self.queue.async {
MDC.global.with(metadata: loggingMetadata) {
self.logger.warning("Struct3::doSomethingElseAsync")
let library = TestLibrary()
Expand All @@ -160,9 +160,9 @@ private struct Struct3 {
group.wait()
MDC.global["foo"] = nil
// only effects the logger instance
var l = logger
var l = self.logger
l[metadataKey: "baz"] = "qux"
l.debug("Struct3::doSomethingElse::Local")
logger.debug("Struct3::doSomethingElse::end")
self.logger.debug("Struct3::doSomethingElse::end")
}
}
6 changes: 3 additions & 3 deletions Tests/LoggingTests/LocalLoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private struct Struct2 {
var c = context
c.logLevel = .info // only effects from this point on
c.logger.info("Struct2::doSomething")
doSomethingElse(context: c)
self.doSomethingElse(context: c)
c.logger.debug("Struct2::doSomething::end")
}

Expand All @@ -131,15 +131,15 @@ private struct Struct3 {
var c = context
c["bar"] = "baz" // only effects from this point on
c.logger.error("Struct3::doSomething")
doSomethingElse(context: c)
self.doSomethingElse(context: c)
c.logger.debug("Struct3::doSomething::end")
}

private func doSomethingElse(context: Context) {
context.logger.error("Struct3::doSomethingElse")
let group = DispatchGroup()
group.enter()
queue.async {
self.queue.async {
context.logger.warning("Struct3::doSomethingElseAsync")
let library = TestLibrary()
library.doSomething()
Expand Down
14 changes: 7 additions & 7 deletions Tests/LoggingTests/LoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class LoggingTest: XCTestCase {
// bootstrap with our test logging impl
let logging1 = TestLogging()
let logging2 = TestLogging()
LoggingSystem.bootstrapInternal({ MultiplexLogHandler([logging1.make(label: $0), logging2.make(label: $0)]) })
LoggingSystem.bootstrapInternal { MultiplexLogHandler([logging1.make(label: $0), logging2.make(label: $0)]) }

var logger = Logger(label: "test")
logger.logLevel = .warning
Expand Down Expand Up @@ -137,13 +137,13 @@ class LoggingTest: XCTestCase {
var logger = Logger(label: "\(#function)")

logger[metadataKey: "foo"] = .stringConvertible("raw-string")
let lazyBox = LazyMetadataBox({ "rendered-at-first-use" })
let lazyBox = LazyMetadataBox { "rendered-at-first-use" }
logger[metadataKey: "lazy"] = .stringConvertible(lazyBox)
logger.info("hello world!")
testLogging.history.assertExist(level: .info,
message: "hello world!",
metadata: ["foo": .stringConvertible("raw-string"),
"lazy": .stringConvertible(LazyMetadataBox({ "rendered-at-first-use" }))])
"lazy": .stringConvertible(LazyMetadataBox { "rendered-at-first-use" })])
}

private func dontEvaluateThisString(file: StaticString = #file, line: UInt = #line) -> Logger.Message {
Expand Down Expand Up @@ -361,7 +361,7 @@ class LoggingTest: XCTestCase {

let logRecorder = Recorder()
LoggingSystem.bootstrapInternal { _ in
return LogHandlerWithGlobalLogLevelOverride(recorder: logRecorder)
LogHandlerWithGlobalLogLevelOverride(recorder: logRecorder)
}

var logger1 = Logger(label: "logger-\(#file):\(#line)")
Expand All @@ -388,7 +388,7 @@ class LoggingTest: XCTestCase {
logRecorder.assertExist(level: .notice, message: "logger1, after")
logRecorder.assertExist(level: .notice, message: "logger2, after")
}

func testLogLevelCases() {
let levels = Logger.Level.allCases
XCTAssertEqual(7, levels.count)
Expand Down Expand Up @@ -424,8 +424,8 @@ class LoggingTest: XCTestCase {

func write(_ string: String) {
// This is a test implementation, a real implementation would include locking
strings.append(string)
interceptedText = (interceptedText ?? "") + string
self.strings.append(string)
self.interceptedText = (self.interceptedText ?? "") + string
}
}

Expand Down
10 changes: 5 additions & 5 deletions Tests/LoggingTests/TestLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal struct TestLogHandler: LogHandler {

func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) {
let metadata = (self._metadataSet ? self.metadata : MDC.global.metadata).merging(metadata ?? [:], uniquingKeysWith: { _, new in new })
logger.log(level: level, message, metadata: metadata, file: file, function: function, line: line)
self.logger.log(level: level, message, metadata: metadata, file: file, function: function, line: line)
self.recorder.record(level: level, metadata: metadata, message: message)
}

Expand Down Expand Up @@ -208,7 +208,7 @@ public class MDC {
}
set {
self.lock.withLock {
if nil == self.storage[self.threadId] {
if self.storage[self.threadId] == nil {
self.storage[self.threadId] = Logger.Metadata()
}
self.storage[self.threadId]![metadataKey] = newValue
Expand Down Expand Up @@ -253,9 +253,9 @@ public class MDC {

private var threadId: Int {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
return Int(pthread_mach_thread_np(pthread_self()))
return Int(pthread_mach_thread_np(pthread_self()))
#else
return Int(pthread_self())
return Int(pthread_self())
#endif
}
}
Expand Down Expand Up @@ -284,7 +284,7 @@ internal struct TestLibrary {
// libraries that use global loggers and async, need to make sure they propagate the
// logging metadata when creating a new thread
let metadata = MDC.global.metadata
queue.asyncAfter(deadline: .now() + 0.1) {
self.queue.asyncAfter(deadline: .now() + 0.1) {
MDC.global.with(metadata: metadata) {
self.logger.info("TestLibrary::doSomethingAsync")
completion()
Expand Down
69 changes: 24 additions & 45 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,56 +1,35 @@
ARG ubuntu_version=18.04
FROM ubuntu:$ubuntu_version
ARG swift_version=5.0
ARG ubuntu_version=bionic
FROM swift:$swift_version-$ubuntu_version
# needed to do again after FROM due to docker limitation
ARG swift_version
ARG ubuntu_version

ARG DEBIAN_FRONTEND=noninteractive
# do not start services during installation as this will fail and log a warning / error.
RUN echo "#!/bin/sh\nexit 0" > /usr/sbin/policy-rc.d
# set as UTF-8
RUN apt-get update && apt-get install -y locales locales-all
ENV LC_ALL en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US.UTF-8

# basic dependencies
RUN apt-get update && apt-get install -y wget git build-essential software-properties-common pkg-config locales
RUN apt-get update && apt-get install -y libicu-dev libblocksruntime0 curl libcurl4-openssl-dev libz-dev

# local
RUN locale-gen en_US.UTF-8
RUN locale-gen en_US en_US.UTF-8
RUN dpkg-reconfigure locales
RUN echo 'export LANG=en_US.UTF-8' >> $HOME/.profile
RUN echo 'export LANGUAGE=en_US:en' >> $HOME/.profile
RUN echo 'export LC_ALL=en_US.UTF-8' >> $HOME/.profile

# known_hosts
RUN mkdir -p $HOME/.ssh
RUN touch $HOME/.ssh/known_hosts
RUN ssh-keyscan github.com 2> /dev/null >> $HOME/.ssh/known_hosts

# clang
RUN apt-get update && apt-get install -y clang-3.9
RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.9 100
RUN update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.9 100
# dependencies
RUN apt-get update && apt-get install -y wget
RUN apt-get update && apt-get install -y lsof dnsutils netcat-openbsd net-tools # used by integration tests

# ruby and jazzy for docs generation
#ARG skip_ruby_from_ppa
#RUN [ -n "$skip_ruby_from_ppa" ] || apt-add-repository -y ppa:brightbox/ruby-ng
#RUN [ -n "$skip_ruby_from_ppa" ] || { apt-get update && apt-get install -y ruby2.4 ruby2.4-dev; }
#RUN [ -z "$skip_ruby_from_ppa" ] || { apt-get update && apt-get install -y ruby ruby-dev; }
#RUN apt-get update && apt-get install -y libsqlite3-dev
RUN apt-get update && apt-get install -y ruby ruby-dev libsqlite3-dev
RUN gem install jazzy --no-ri --no-rdoc

# swift
ARG swift_version=4.2.3
ARG swift_flavour=RELEASE
ARG swift_builds_suffix=release
# tools
RUN mkdir -p $HOME/.tools
RUN echo 'export PATH="$HOME/.tools:$PATH"' >> $HOME/.profile

# script to allow mapping framepointers on linux (until part of the toolchain)
RUN wget -q https://raw.githubusercontent.com/apple/swift/master/utils/symbolicate-linux-fatal -O $HOME/.tools/symbolicate-linux-fatal
RUN chmod 755 $HOME/.tools/symbolicate-linux-fatal

RUN mkdir $HOME/.swift
RUN wget -q "https://swift.org/builds/swift-${swift_version}-${swift_builds_suffix}/ubuntu$(echo $ubuntu_version | sed 's/\.//g')/swift-${swift_version}-${swift_flavour}/swift-${swift_version}-${swift_flavour}-ubuntu${ubuntu_version}.tar.gz" -O $HOME/swift.tar.gz
RUN tar xzf $HOME/swift.tar.gz --directory $HOME/.swift --strip-components=1
RUN echo 'export PATH="$HOME/.swift/usr/bin:$PATH"' >> $HOME/.profile
RUN echo 'export LINUX_SOURCEKIT_LIB_PATH="$HOME/.swift/usr/lib"' >> $HOME/.profile
# swiftformat (until part of the toolchain)

# script to allow mapping framepointers on linux
RUN mkdir -p $HOME/.scripts
RUN wget -q https://raw.githubusercontent.com/apple/swift/master/utils/symbolicate-linux-fatal -O $HOME/.scripts/symbolicate-linux-fatal
RUN chmod 755 $HOME/.scripts/symbolicate-linux-fatal
RUN echo 'export PATH="$HOME/.scripts:$PATH"' >> $HOME/.profile
ARG swiftformat_version=0.40.11
RUN git clone --branch $swiftformat_version --depth 1 https://github.com/nicklockwood/SwiftFormat $HOME/.tools/swift-format
RUN cd $HOME/.tools/swift-format && swift build -c release
RUN ln -s $HOME/.tools/swift-format/.build/release/swiftformat $HOME/.tools/swiftformat
2 changes: 1 addition & 1 deletion docker/docker-compose.1804.50.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
image: swift-log:18.04-5.0
build:
args:
ubuntu_version: "18.04"
ubuntu_version: "bionic"
swift_version: "5.0"

test:
Expand Down
8 changes: 4 additions & 4 deletions scripts/generate_linux_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ def createLinuxMain(testsDirectory, allTestSubDirectories, files)

file.write "#if os(Linux) || os(FreeBSD)\n"
for testSubDirectory in allTestSubDirectories.sort { |x, y| x <=> y }
file.write ' @testable import ' + testSubDirectory + "\n"
file.write '@testable import ' + testSubDirectory + "\n"
end
file.write "\n"
file.write " XCTMain([\n"
file.write "XCTMain([\n"

testCases = []
for classes in files
Expand All @@ -109,9 +109,9 @@ def createLinuxMain(testsDirectory, allTestSubDirectories, files)
end

for testCase in testCases.sort { |x, y| x <=> y }
file.write ' testCase(' + testCase + ".allTests),\n"
file.write ' testCase(' + testCase + ".allTests),\n"
end
file.write " ])\n"
file.write "])\n"
file.write "#endif\n"
end
end
Expand Down
15 changes: 14 additions & 1 deletion scripts/sanity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,23 @@ else
printf "\033[0;32mokay.\033[0m\n"
fi

printf "=> Checking license headers... "
printf "=> Checking format... "
FIRST_OUT="$(git status --porcelain)"
swiftformat . > /dev/null 2>&1
SECOND_OUT="$(git status --porcelain)"
if [[ "$FIRST_OUT" != "$SECOND_OUT" ]]; then
printf "\033[0;31mformatting issues!\033[0m\n"
git --no-pager diff
exit 1
else
printf "\033[0;32mokay.\033[0m\n"
fi

printf "=> Checking license headers\n"
tmp=$(mktemp /tmp/.swift-log-sanity_XXXXXX)

for language in swift-or-c bash dtrace; do
printf " * $language... "
declare -a matching_files
declare -a exceptions
expections=( )
Expand Down

0 comments on commit 2637350

Please sign in to comment.