Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyome22 committed Apr 21, 2021
1 parent 2107a90 commit d6c26f2
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "LineCounter",
platforms: [
.macOS(.v10_15)
],
products: [
.executable(name: "lc", targets: ["LineCounter"])
],
targets: [
.target(
name: "LineCounter",
dependencies: []),
.testTarget(
name: "LineCounterTests",
dependencies: ["LineCounter"]),
]
)
21 changes: 21 additions & 0 deletions Sources/LineCounter/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// main.swift
// LineCounter
//
// Created by ky0me22 on 2021/04/21.
//

import Foundation

guard CommandLine.arguments.count == 2 else {
print("usage: lc [absolute path of a file]")
exit(1)
}

let fileUrl = URL(fileURLWithPath: CommandLine.arguments[1])
guard let file = try? String(contentsOf: fileUrl, encoding: .utf8) else {
print("Error: could not read \(fileUrl.absoluteString)")
exit(2)
}

print(file.components(separatedBy: CharacterSet.newlines).count)
19 changes: 19 additions & 0 deletions Sources/LineCounter/manual.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// File.swift
//
//
// Created by ky0me22 on 2021/04/21.
//

let manual = """
Usage:
lc [-p <arg>...] [options]
lc -h|--help
Options:
-p, --path PATH Specify an absolute path for the file or directory
-e, --extension EXTENSION Specify a file extension for which you want to
count the number of lines
"""
47 changes: 47 additions & 0 deletions Tests/LineCounterTests/LineCounterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import XCTest
import class Foundation.Bundle

final class LineCounterTests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.

// Some of the APIs that we use below are available in macOS 10.13 and above.
guard #available(macOS 10.13, *) else {
return
}

let fooBinary = productsDirectory.appendingPathComponent("LineCounter")

let process = Process()
process.executableURL = fooBinary

let pipe = Pipe()
process.standardOutput = pipe

try process.run()
process.waitUntilExit()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)

XCTAssertEqual(output, "Hello, world!\n")
}

/// Returns path to the built products directory.
var productsDirectory: URL {
#if os(macOS)
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
return bundle.bundleURL.deletingLastPathComponent()
}
fatalError("couldn't find the products directory")
#else
return Bundle.main.bundleURL
#endif
}

static var allTests = [
("testExample", testExample),
]
}
9 changes: 9 additions & 0 deletions Tests/LineCounterTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import XCTest

#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(LineCounterTests.allTests),
]
}
#endif
7 changes: 7 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import XCTest

import LineCounterTests

var tests = [XCTestCaseEntry]()
tests += LineCounterTests.allTests()
XCTMain(tests)

0 comments on commit d6c26f2

Please sign in to comment.