Skip to content

Commit ccad5ad

Browse files
committed
Add basic pkgconfig parser test
1 parent 3ac6547 commit ccad5ad

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

Sources/Build/PkgConfig.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct PkgConfig {
6161
return []
6262
}
6363

64-
private static func locatePCFile(name: String) throws -> String {
64+
static func locatePCFile(name: String) throws -> String {
6565
for path in (searchPaths + envSearchPaths) {
6666
let pcFile = Path.join(path, "\(name).pc")
6767
if pcFile.isFile {
@@ -72,14 +72,15 @@ struct PkgConfig {
7272
}
7373
}
7474

75-
private struct PkgConfigParser {
76-
let pcFile: String
77-
var variables = [String: String]()
75+
struct PkgConfigParser {
76+
private let pcFile: String
77+
private(set) var variables = [String: String]()
7878
var dependencies = [String]()
7979
var cFlags = ""
8080
var libs = ""
8181

8282
init(pcFile: String) {
83+
precondition(pcFile.isFile)
8384
self.pcFile = pcFile
8485
}
8586

@@ -101,7 +102,7 @@ private struct PkgConfigParser {
101102
}
102103
}
103104

104-
func parseDependencies(_ depString: String) -> [String] {
105+
private func parseDependencies(_ depString: String) -> [String] {
105106
let exploded = depString.characters.split(separator: " ").map(String.init)
106107
let operators = ["=", "<", ">", "<=", ">="]
107108
var deps = [String]()
@@ -120,7 +121,7 @@ private struct PkgConfigParser {
120121
return deps
121122
}
122123

123-
func resolveVariables(_ line: String) -> String {
124+
private func resolveVariables(_ line: String) -> String {
124125
func resolve(_ string: String) -> String {
125126
var resolvedString = string
126127
guard let dollar = resolvedString.characters.index(of: "$") else { return string }
@@ -137,7 +138,7 @@ private struct PkgConfigParser {
137138
return resolved
138139
}
139140

140-
func value(line: String) -> String {
141+
private func value(line: String) -> String {
141142
guard let colonIndex = line.characters.index(of: ":") else {
142143
return ""
143144
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
@testable import Build
12+
@testable import Utility
13+
import XCTest
14+
15+
final class PkgConfigParserTests: XCTestCase {
16+
17+
func testGTK3PCFile() {
18+
loadPCFile("gtk+-3.0.pc") { parser in
19+
XCTAssertEqual(parser.variables, ["libdir": "/usr/local/Cellar/gtk+3/3.18.9/lib", "gtk_host": "x86_64-apple-darwin15.3.0", "includedir": "/usr/local/Cellar/gtk+3/3.18.9/include", "prefix": "/usr/local/Cellar/gtk+3/3.18.9", "gtk_binary_version": "3.0.0", "exec_prefix": "/usr/local/Cellar/gtk+3/3.18.9", "targets": "quartz"])
20+
XCTAssertEqual(parser.dependencies, ["gdk-3.0", "atk", "cairo", "cairo-gobject", "gdk-pixbuf-2.0", "gio-2.0"])
21+
XCTAssertEqual(parser.cFlags, "-I/usr/local/Cellar/gtk+3/3.18.9/include/gtk-3.0 ")
22+
XCTAssertEqual(parser.libs, "-L/usr/local/Cellar/gtk+3/3.18.9/lib -lgtk-3 ")
23+
}
24+
}
25+
26+
private func loadPCFile(_ inputName: String, line: UInt = #line, body: (PkgConfigParser) -> Void) {
27+
do {
28+
let input = Path.join(#file, "../pkgconfigInputs", inputName).normpath
29+
var parser = PkgConfigParser(pcFile: input)
30+
try parser.parse()
31+
body(parser)
32+
} catch {
33+
XCTFail("Unexpected error: \(error)", file: #file, line: line)
34+
}
35+
}
36+
}
37+
38+
39+
extension PkgConfigParserTests {
40+
static var allTests : [(String, PkgConfigParserTests -> () throws -> Void)] {
41+
return []
42+
}
43+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
prefix=/usr/local/Cellar/gtk+3/3.18.9
2+
exec_prefix=${prefix}
3+
libdir=${exec_prefix}/lib
4+
includedir=${prefix}/include
5+
targets=quartz
6+
7+
gtk_binary_version=3.0.0
8+
gtk_host=x86_64-apple-darwin15.3.0
9+
10+
Name: GTK+
11+
Description: GTK+ Graphical UI Library
12+
Version: 3.18.9
13+
Requires: gdk-3.0 atk >= 2.15.1 cairo >= 1.14.0 cairo-gobject >= 1.14.0 gdk-pixbuf-2.0 >= 2.30.0 gio-2.0 >= 2.45.8
14+
Requires.private: atk epoxy >= 1.0 gio-unix-2.0 >= 2.45.8
15+
Libs: -L${libdir} -lgtk-3
16+
Cflags: -I${includedir}/gtk-3.0

0 commit comments

Comments
 (0)