Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 241a65b

Browse files
authored
Merge pull request #451 from kazk/rewrite-swift
Rewrite Swift runner
2 parents f6c48ed + 2cd6a60 commit 241a65b

File tree

10 files changed

+311
-1120
lines changed

10 files changed

+311
-1120
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ env:
2828
- TEST_IMG=dart
2929
- TEST_IMG=crystal
3030
- TEST_IMG=ocaml
31-
# - TEST_IMG=swift
31+
- TEST_IMG=swift
3232
- TEST_IMG=haskell
3333
- TEST_IMG=objc
3434
- TEST_IMG=go

docker/swift.docker

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,73 @@
1-
# EXAMPLE USAGE: docker run --privileged --rm codewars/swift-runner run -l swift -c "print(1+1)"
2-
3-
# Pull base image.
1+
# EXAMPLE USAGE: docker run --rm codewars/swift-runner run -l swift -c "print(1+1)"
42
FROM codewars/base-runner
53

6-
# Optionally mount '/swift-vol' on ephemeral storage vol for faster
7-
# installing of packages, compiling, etc.
8-
env SWIFT_VOL /swift-vol
9-
env SWIFT_GEN_DIR $SWIFT_VOL/swift-gen
10-
env SWIFT_SOURCE_DIR $SWIFT_VOL/swift-source
11-
12-
# Install required dependencies for building Swift.
13-
# See https://github.com/apple/swift
14-
RUN apt-get update
15-
RUN apt-get -y upgrade
16-
RUN apt-get install -y curl rsync cmake git ninja-build clang python uuid-dev libicu-dev icu-devtools libbsd-dev libedit-dev libxml2-dev libsqlite3-dev swig libpython-dev libncurses5-dev pkg-config libblocksruntime-dev libcurl4-openssl-dev
17-
18-
# Install cmake (Cmake version must be at least 3.4.3)
19-
RUN wget http://www.cmake.org/files/v3.5/cmake-3.5.2.tar.gz
20-
RUN tar xzf cmake-3.5.2.tar.gz
21-
RUN cd cmake-3.5.2 && ./configure && make && sudo make install
22-
RUN sudo rm -f /usr/bin/cmake
23-
RUN sudo ln -s /usr/local/bin/cmake /usr/bin/cmake
24-
25-
# Install clang
26-
RUN apt-get install -y clang-3.6
27-
RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.6 100
28-
RUN update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100
29-
30-
# Downalod all Swift related Git repos.
31-
# See https://github.com/apple/swift
32-
WORKDIR $SWIFT_SOURCE_DIR
33-
RUN git clone https://github.com/apple/swift.git && ./swift/utils/update-checkout --clone
34-
RUN git clone https://github.com/ninja-build/ninja.git && cd ninja && git checkout release
35-
36-
# Copy our custom codewars standard output files to the swift-corelibs-xctest directory.
37-
COPY frameworks/swift/xctest/PrintObserver.swift $SWIFT_SOURCE_DIR/swift-corelibs-xctest/Sources/XCTest/Private/PrintObserver.swift
38-
COPY frameworks/swift/xctest/XCTAssert.swift $SWIFT_SOURCE_DIR/swift-corelibs-xctest/Sources/XCTest/Public/XCTAssert.swift
39-
40-
# Copy our custom build-presets file to the swift directory.
41-
COPY frameworks/swift/xctest/codewars-build-presets.ini $SWIFT_SOURCE_DIR/swift/codewars-build-presets.ini
42-
43-
# Build Swift
44-
WORKDIR $SWIFT_SOURCE_DIR/swift
45-
RUN utils/build-script --preset-file=codewars-build-presets.ini --preset=codewars_linux_1404 install_destdir=$SWIFT_GEN_DIR installable_package=$SWIFT_GEN_DIR/swift.tar.gz
46-
env PATH $SWIFT_GEN_DIR/usr/bin:$PATH
47-
48-
# Remove swift source code to save space on the image.
49-
RUN rm -rf $SWIFT_SOURCE_DIR/*
50-
51-
# Change permissions so we maybe don't have to use the --privileged docker run flag.
52-
RUN chmod -R a+rx $SWIFT_VOL/*
4+
# Based on https://github.com/swiftdocker/docker-swift/blob/master/Dockerfile
5+
RUN apt-get -q update && \
6+
apt-get -q install -y --no-install-recommends \
7+
clang-3.6 \
8+
curl \
9+
git \
10+
libc6-dev \
11+
libedit-dev \
12+
libicu-dev \
13+
libssl-dev \
14+
libcurl4-openssl-dev \
15+
libxml2 \
16+
pkg-config \
17+
python2.7 \
18+
python2.7-dev \
19+
make \
20+
&& update-alternatives --quiet --install /usr/bin/clang clang /usr/bin/clang-3.6 100 \
21+
&& update-alternatives --quiet --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.6 100 \
22+
&& rm -r /var/lib/apt/lists/*
23+
24+
RUN set -ex \
25+
&& export GNUPGHOME="$(mktemp -d)" \
26+
&& \
27+
for key in \
28+
# pub 4096R/412B37AD 2015-11-19 [expires: 2017-11-18]
29+
# Key fingerprint = 7463 A81A 4B2E EA1B 551F FBCF D441 C977 412B 37AD
30+
# uid Swift Automatic Signing Key #1 <[email protected]>
31+
7463A81A4B2EEA1B551FFBCFD441C977412B37AD \
32+
# pub 4096R/21A56D5F 2015-11-28 [expires: 2017-11-27]
33+
# Key fingerprint = 1BE1 E29A 084C B305 F397 D62A 9F59 7F4D 21A5 6D5F
34+
# uid Swift 2.2 Release Signing Key <swift-[email protected]>
35+
1BE1E29A084CB305F397D62A9F597F4D21A56D5F \
36+
# pub 4096R/91D306C6 2016-05-31 [expires: 2018-05-31]
37+
# Key fingerprint = A3BA FD35 56A5 9079 C068 94BD 63BC 1CFE 91D3 06C6
38+
# uid Swift 3.x Release Signing Key <[email protected]>
39+
A3BAFD3556A59079C06894BD63BC1CFE91D306C6 \
40+
; do \
41+
gpg --quiet --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
42+
done \
43+
&& cd /tmp \
44+
&& curl -fSsL https://swift.org/builds/swift-3.1.1-release/ubuntu1404/swift-3.1.1-RELEASE/swift-3.1.1-RELEASE-ubuntu14.04.tar.gz -o swift.tar.gz \
45+
&& curl -fSsL https://swift.org/builds/swift-3.1.1-release/ubuntu1404/swift-3.1.1-RELEASE/swift-3.1.1-RELEASE-ubuntu14.04.tar.gz.sig -o swift.tar.gz.sig \
46+
&& gpg --batch --verify --quiet swift.tar.gz.sig swift.tar.gz \
47+
&& tar -xzf swift.tar.gz --directory / --strip-components=1 \
48+
&& rm -r "$GNUPGHOME" swift.tar.gz.sig swift.tar.gz \
49+
&& rm -rf /var/lib/apt/lists/* \
50+
&& chmod -R o+r /usr/lib/swift
5351

5452
# Add the package json first to a tmp directory and build, copy over so that we dont rebuild every time
55-
ADD package.json /tmp/package.json
56-
RUN cd /tmp && npm install --production
57-
RUN mkdir -p /runner && cp -a /tmp/node_modules /runner
53+
COPY package.json /tmp/package.json
54+
WORKDIR /tmp
55+
RUN npm install --production \
56+
&& mkdir -p /runner \
57+
&& cp -a /tmp/node_modules /runner \
58+
&& rm -rf /tmp/node_modules
5859

5960
# ADD cli-runner and install node deps
6061
ADD . /runner
6162

6263
WORKDIR /runner
6364
RUN ln -s /home/codewarrior /workspace
6465

65-
# Run the test suite to make sure this thing works
66-
6766
USER codewarrior
68-
6967
# Set environment variables
70-
ENV USER codewarrior
71-
ENV HOME /home/codewarrior
68+
ENV USER=codewarrior HOME=/home/codewarrior
69+
70+
# Run the test suite to make sure this thing works
7271
RUN mocha -t 5000 test/runners/swift_spec.js
7372

7473
# timeout is a fallback in case an error with node

examples/swift.yml

Lines changed: 71 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
cw-2:
1+
xctest:
22
algorithms:
33
initial: |-
44
// return the two oldest/oldest ages within the array of ages passed in.
55
// it should return the two ages as a sorted array, youngest age first
6-
func twoOldestAges(ages: [Int]) -> [Int] {
7-
// TODO: complete
6+
func twoOldestAges(ages: [Int]) -> [Int] { // TODO: complete
87
}
98
109
answer: |-
@@ -21,102 +20,102 @@ cw-2:
2120
return [nextOldest, oldest]
2221
}
2322
24-
2523
fixture: |-
26-
// some example data
27-
let results1 = twoOldestAges(ages: [1,5,87,45,8,8])
28-
let results2 = twoOldestAges(ages: [6,5,83,5,3,18])
29-
30-
// NOTE: You can use Test or test, whichever you prefer.
31-
32-
// Use "describe" to label your test suite.
33-
Test.describe("twoOldestAges")
34-
35-
// Use "it" to identify the conditions you are testing for
36-
Test.it("should return the second oldest age first")
37-
// using assert_equals will report the invalid values to the user
38-
Test.assert_equals(results1[0], 45)
39-
// using expect will just give a user a generic error message, unless you provide a message
40-
Test.expect(results2[0] == 18, "Number is not the second oldest")
24+
import XCTest
4125
42-
// its best practice to test for multiple groups of tests, using it calls.
43-
Test.it("should return the oldest age last")
26+
class TwoOldestAgesTest: XCTestCase {
27+
static var allTests = [
28+
("testReturnSecondOldestFirst", testReturnSecondOldestFirst),
29+
("testReturnOldestSecond", testReturnOldestSecond),
30+
]
31+
func testReturnSecondOldestFirst() {
32+
let r1 = twoOldestAges(ages: [1,5,87,45,8,8])
33+
XCTAssertEqual(r1[0], 45)
34+
let r2 = twoOldestAges(ages: [6,5,83,5,3,18])
35+
XCTAssertEqual(r2[0], 18)
36+
}
37+
func testReturnOldestSecond() {
38+
let r1 = twoOldestAges(ages: [1,5,87,45,8,8])
39+
XCTAssertEqual(r1[1], 87)
40+
let r2 = twoOldestAges(ages: [6,5,83,5,3,18])
41+
XCTAssertEqual(r2[1], 83)
42+
}
43+
}
4444
45-
Test.assert_equals(results1[1], 87)
46-
Test.expect(results2[1] == 83, "Number is not the oldest")
45+
_XCTMain([
46+
testCase(TwoOldestAgesTest.allTests)
47+
])
4748
4849
bug fixes:
4950
initial: |-
50-
func add(a: Int, b: Int) -> Int {
51+
func add(_ a: Int, _ b: Int) -> Int {
5152
a + b
5253
}
5354
5455
answer: |-
55-
func add(a: Int, b: Int) -> Int {
56+
func add(_ a: Int, _ b: Int) -> Int {
5657
return a + b
5758
}
5859
5960
fixture: |-
60-
// Use "describe" to define the test suite
61-
Test.describe("add method")
61+
import XCTest
6262
63-
// Use "it" to indicate a condition you are testing for
64-
Test.it("should add both arguments and return")
63+
class AddTest: XCTestCase {
64+
static var allTests = [
65+
("testAdd", testAdd),
66+
]
67+
func testAdd() {
68+
XCTAssertEqual(add(1, 1), 2)
69+
}
70+
}
6571
66-
// "assert_equals" will return information about what values were
67-
// expect if the assertion fails. This can be very useful to other
68-
// users trying to pass the kata.
69-
Test.assert_equals(add(a:1,b:2), 3)
72+
_XCTMain([
73+
testCase(AddTest.allTests)
74+
])
7075
71-
// "expect" is a lower level assertion that will allow you to test
72-
// anything. It just needs a boolean result. You should pass a message
73-
// as the second parameter so that if the assertion fails the user
74-
// will be giving some useful information.
75-
Test.expect(add(a:1,b:1) == 2, "add(a:1,b:1) should == 2")
7676
7777
refactoring:
7878
initial: |-
7979
// refactor this method into a Person class with its own greet method
80+
// let p = Person("foo")
81+
// p.name == "foo"
82+
// p.greet("bar") == "Hello bar, my name is foo"
8083
func greet(other: String, name: String) -> String {
8184
return "Hello \(other), my name is \(name)"
8285
}
8386
8487
answer: |-
8588
class Person {
8689
let name: String
87-
init(name: String) {
90+
init(_ name: String) {
8891
self.name = name
8992
}
90-
func greet(other: String) -> String {
93+
func greet(_ other: String) -> String {
9194
return "Hello \(other), my name is \(self.name)"
9295
}
9396
}
9497
9598
fixture: |-
96-
// Use "describe" to define the test suite
97-
Test.describe("Person")
98-
99-
let jack = Person(name: "Jack")
100-
101-
// Use "it" to indicate a condition you are testing for
102-
Test.it("should have a name")
103-
104-
// "assert_equals" will return information about what values were
105-
// expected if the assertion fails. This can be very useful to other
106-
// users trying to pass the kata.
107-
Test.assert_equals(jack.name, "Jack")
108-
109-
Test.it("should greet Jill")
110-
111-
Test.assert_equals(jack.greet(other: "Jill"), "Hello Jill, my name is Jack")
112-
113-
Test.it("should greet other people as well")
99+
import XCTest
100+
class PersonTest: XCTestCase {
101+
static var allTests = [
102+
("testName", testName),
103+
("testGreet", testGreet),
104+
]
105+
func testName() {
106+
let jack = Person("Jack")
107+
XCTAssertEqual(jack.name, "Jack")
108+
}
109+
func testGreet() {
110+
let jack = Person("Jack")
111+
XCTAssertEqual(jack.greet("Jill"), "Hello Jill, my name is Jack")
112+
XCTAssertEqual(jack.greet("Jane"), "Hello Jane, my name is Jack")
113+
}
114+
}
114115
115-
// unlike "assert_equals", "expect" is a lower level assertion that
116-
// takes a boolean to determine if it passes. If it fails it will
117-
// output the message that you give it, or a generic one. It is a good
118-
// idea to provide a custom error message to help users pass the kata
119-
Test.expect(jack.greet(other: "Jane") == "Hello Jane, my name is Jack", "Jack apparently is only able to greet Jane")
116+
_XCTMain([
117+
testCase(PersonTest.allTests)
118+
])
120119
121120
reference:
122121
initial: |-
@@ -125,62 +124,18 @@ cw-2:
125124
answer: |-
126125
let websites = ["codewars"]
127126
128-
fixture: |-
129-
// Use test.describe (or Test.describe) to describe your test suite
130-
Test.describe("websites")
131-
132-
// Use "it" calls to describe the specific test case
133-
Test.it("should have the value 'codewars' inside of it")
134-
135-
// assert equals will pass if both items equal each other (using ==). If
136-
// the test fails, assert_equals will output a descriptive message indicating
137-
// what the values were expected to be.
138-
Test.assert_equals(["codewars"], websites)
139-
140-
// you can also use the lower level test.expect. If you use test.expect directly then
141-
// you should provide a custom error message, as the default one will be pretty useless
142-
// to users trying to pass the kata.
143-
Test.expect(["codewars"] == websites, "Array does not have correct value")
144-
xctest:
145-
reference:
146-
initial: |-
147-
class Person {
148-
let name: String
149-
150-
init(_ name: String) {
151-
// TODO: Program Constructor
152-
}
153-
154-
func greet(_ other: String) -> String {
155-
// TODO: Write a greeting string
156-
}
157-
}
158-
159-
answer: |-
160-
class Person {
161-
let name: String
162-
163-
init(_ name: String) {
164-
self.name = name
165-
}
166-
167-
func greet(_ other: String) -> String {
168-
return "Hello, \(other), I am \(name), it's nice to meet you!"
169-
}
170-
}
171-
172127
fixture: |-
173128
import XCTest
174129
175-
class PersonTest: XCTestCase {
130+
class WebsitesTest: XCTestCase {
176131
static var allTests = [
177-
("testGreet", testGreet),
132+
("testDefined", testDefined),
178133
]
179-
func testGreet() {
180-
let person = Person("Jorge")
181-
XCTAssertEqual(person.greet("Aditya"), "Hello, Aditya, I am Jorge, it's nice to meet you!")
134+
func testDefined() {
135+
XCTAssertEqual(websites[0], "codewars")
182136
}
183-
}
184-
XCTMain([
185-
testCase(PersonTest.allTests)
186-
])
137+
}
138+
139+
_XCTMain([
140+
testCase(WebsitesTest.allTests)
141+
])

0 commit comments

Comments
 (0)