1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| import FilesProvider
var documentsProvider:LocalFileProvider!
var pngexpectation: XCTestExpectation! = nil
override func setUp() {
// Put setup code here. This method is called before the invocation of each test method in the class.
documentsProvider = LocalFileProvider(for: .userDirectory, in: .allDomainsMask)
pngexpectation = self.expectation(description: "BLDownloadImageNotification")
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
//创建文件夹
documentsProvider.create(folder: "testPng", at: "admin/hsg/") { (err) in
print("新建目录成功")
//拷贝图片到目录
self.documentsProvider.contentsOfDirectory(path: "admin/Desktop",
completionHandler: { (contents, error) in
for file in contents
{
let png = file.path.hasSuffix("png")
if png{
print("文件路径:\(file.path)")
let thePath:NSString = file.path as NSString
let toFile = "admin/hsg/testPng/\(thePath.lastPathComponent)"
self.documentsProvider.copyItem(path: file.path, to: toFile, overwrite: true, completionHandler: { (err) in
//调用shell工具压缩图片
self.testProcessRunShellScript(filePath: toFile)
print("Name: \(file.name)")
print("Size: \(file.size)")
print("路径: \(file.path)")
})
}
}
})
}
waitForExpectations(timeout: 40, handler: nil)
}
//可用
func testProcessRunShellScript(filePath:String) {
let sub = "_temp.png"
let pngquantFile = "/Users/"+filePath
let tmpFilePath = filePath.replacingOccurrences(of: ".png", with: sub)
let exePath = "/Users/admin/hsg/hexo/GitSubmodules/hsgTool/pngquant/pngquant"
Process.launchedProcess(launchPath: exePath, arguments: ["--ext",sub,"--speed=3",pngquantFile])
// 删除旧文件
// documentsProvider.removeItem(path: filePath) { (error) in
// //重命名压缩过的tmp文件
// print("ddddddderr:\(error?.localizedDescription)")
// self.documentsProvider.moveItem(path: tmpFilePath, to: filePath, overwrite: true, completionHandler: { (error) in
// self.pngexpectation.fulfill()
// })
// }
}
|