<commit_before>//
//  TaylorIntegrationTest.swift
//  TaylorIntegrationTest
//
//  Created by <NAME> on 11/9/15.
//  Copyright © 2015 YOPESO. All rights reserved.
//

import Quick
import Nimble
import TaylorFramework

class TaylorIntegrationTests: QuickSpec {
    
    let runTaskPath = NSProcessInfo.processInfo().environment["PWD"]! + "/Taylor.app/Contents/MacOS/Taylor"
    
    var analyzeFilesPath = String()
    var resultFilePath = String()
    
    let resultFileName = "taylor_report.json"
    let fileNames = ["Wrapper.swift", "WrapperProxy.swift", "CloseDialogWindowOperation.swift"]

    
    func cleanOrDestroyResources() throws {
        if NSFileManager.defaultManager().fileExistsAtPath(analyzeFilesPath) {
            try NSFileManager.defaultManager().removeItemAtPath(analyzeFilesPath)
        }
        if NSFileManager.defaultManager().fileExistsAtPath(resultFilePath.stringWithoutLastComponent()) {
            try NSFileManager.defaultManager().removeItemAtPath(resultFilePath.stringWithoutLastComponent())
        }
    }
    
    
    func initializePaths() {
        let mainBundle = NSBundle(forClass: self.dynamicType)
        analyzeFilesPath = mainBundle.resourcePath!.stringByAppendingPathComponent("AnalyzeFiles")
        resultFilePath = mainBundle.resourcePath!.stringByAppendingPathComponent("ResultFiles")
    }
    
    
    func createResources() throws {
        let fileManager = NSFileManager.defaultManager()
        let mainBundle = NSBundle(forClass: self.dynamicType)
        let bundle = NSBundle(path: mainBundle.pathForResource("TaylorIntegrationTestResources", ofType: "bundle")!)!
        if !fileManager.fileExistsAtPath(analyzeFilesPath) {
            try NSFileManager.defaultManager().createDirectoryAtPath(analyzeFilesPath, withIntermediateDirectories: false, attributes: nil)
        }
        if !fileManager.fileExistsAtPath(resultFilePath) {
            try NSFileManager.defaultManager().createDirectoryAtPath(resultFilePath, withIntermediateDirectories: false, attributes: nil)
        }
        try createFiles(bundle)
        try createResultFile(bundle)
    }
    
    
    private func createFiles(sourceBundle: NSBundle) throws {
        for fileName in fileNames {
            let path = sourceBundle.pathForResource(fileName.stringByTrimmingTheExtension, ofType: fileName.fileExtension)!
            let toPath = self.analyzeFilesPath.stringByAppendingPathComponent(path.lastPathComponent)
            if !NSFileManager.defaultManager().fileExistsAtPath(toPath) {
                try NSFileManager.defaultManager().copyItemAtPath(path, toPath: toPath)
            }
        }
    }
    
    
    private func createResultFile(sourceBundle: NSBundle) throws {
        let path = sourceBundle.pathForResource(resultFileName.stringByTrimmingTheExtension, ofType: resultFileName.fileExtension)!
        resultFilePath = resultFilePath.stringByAppendingPathComponent(resultFileName)
        try NSFileManager.defaultManager().copyItemAtPath(path, toPath: self.resultFilePath)
    }
    
    
    override func spec() {
        
        self.initializePaths()
        
        describe("Taylor") {
            
            beforeEach {
                do { try self.createResources() }
                catch let error { print(error) }
            }
            
            
            afterEach {
                do { try self.cleanOrDestroyResources() }
                catch let error { print(error) }
            }
            
            it("should generate correct reports") {
                let runTask = NSTask()
                runTask.launchPath = self.runTaskPath
                runTask.arguments = ["-p", self.analyzeFilesPath, "-r", "json:taylor_report.json"]
                runTask.launch()
                runTask.waitUntilExit()
                let reporterPath = self.analyzeFilesPath.stringByAppendingPathComponent("taylor_report.json")
                expect(ReporterComparator().compareReporters(self.resultFilePath, secondReporterPath: reporterPath)).to(beTrue())
                do {
                    if NSFileManager.defaultManager().fileExistsAtPath(reporterPath) {
                        try NSFileManager.defaultManager().removeItemAtPath(reporterPath)
                    }
                } catch let error { print(error) }<commit_msg>Treat some edge cases in tests by throwing exceptions<commit_after>//
//  TaylorIntegrationTest.swift
//  TaylorIntegrationTest
//
//  Created by <NAME> on 11/9/15.
//  Copyright © 2015 YOPESO. All rights reserved.
//

import Quick
import Nimble
import TaylorFramework

class TaylorIntegrationTests: QuickSpec {
    
    let runTaskPath = NSProcessInfo.processInfo().environment["PWD"]! + "/Taylor.app/Contents/MacOS/Taylor"
    
    var analyzeFilesPath = String()
    var resultFilePath = String()
    
    let resultFileName = "taylor_report.json"
    let fileNames = ["Wrapper.swift", "WrapperProxy.swift", "CloseDialogWindowOperation.swift"]

    
    func cleanOrDestroyResources() throws {
        if NSFileManager.defaultManager().fileExistsAtPath(analyzeFilesPath) {
            try NSFileManager.defaultManager().removeItemAtPath(analyzeFilesPath)
        }
        if NSFileManager.defaultManager().fileExistsAtPath(resultFilePath.stringWithoutLastComponent()) {
            try NSFileManager.defaultManager().removeItemAtPath(resultFilePath.stringWithoutLastComponent())
        }
    }
    
    
    func initializePaths() throws {
        if let resourcePath = NSBundle(forClass: self.dynamicType).resourcePath {
        analyzeFilesPath = resourcePath.stringByAppendingPathComponent("AnalyzeFiles")
        resultFilePath = resourcePath.stringByAppendingPathComponent("ResultFiles")
        } else {
            throw TestError.BundleResourcePathNotFound
        }
    }
    
    
    func createResources() throws {
        let fileManager = NSFileManager.defaultManager()
        let mainBundle = NSBundle(forClass: self.dynamicType)
        let bundle = NSBundle(path: mainBundle.pathForResource("TaylorIntegrationTestResources", ofType: "bundle")!)!
        if !fileManager.fileExistsAtPath(analyzeFilesPath) {
            try NSFileManager.defaultManager().createDirectoryAtPath(analyzeFilesPath, withIntermediateDirectories: false, attributes: nil)
        }
        if !fileManager.fileExistsAtPath(resultFilePath) {
            try NSFileManager.defaultManager().createDirectoryAtPath(resultFilePath, withIntermediateDirectories: false, attributes: nil)
        }
        try createFiles(bundle)
        try createResultFile(bundle)
    }
    
    
    private func createFiles(sourceBundle: NSBundle) throws {
        for fileName in fileNames {
            let path = sourceBundle.pathForResource(fileName.stringByTrimmingTheExtension, ofType: fileName.fileExtension)!
            let toPath = self.analyzeFilesPath.stringByAppendingPathComponent(path.lastPathComponent)
            if !NSFileManager.defaultManager().fileExistsAtPath(toPath) {
                try NSFileManager.defaultManager().copyItemAtPath(path, toPath: toPath)
            }
        }
    }
    
    
    private func createResultFile(sourceBundle: NSBundle) throws {
        let filename = resultFileName.stringByTrimmingTheExtension
        if let path = sourceBundle.pathForResource(filename, ofType: resultFileName.fileExtension) {
        resultFilePath = resultFilePath.stringByAppendingPathComponent(resultFileName)
            try NSFileManager.defaultManager().copyItemAtPath(path, toPath: self.resultFilePath)
        } else {
            throw TestError.FileNotFound(filename)
        }
    }
    
    
    enum TestError: ErrorType {
        case FileNotFound(String)
        case BundleResourcePathNotFound
    }
    
    override func spec() {
        do {
            try self.initializePaths()
        } catch _ {
            print("Error occured while initializing paths.")
        }
        
        describe("Taylor") {
            
            beforeEach {
                do { try self.createResources() }
                catch let error { print(error) }
            }
            
            
            afterEach {
                do { try self.cleanOrDestroyResources() }
                catch let error { print(error) }
            }
            
            it("should generate correct reports") {
                let runTask = NSTask()
                runTask.launchPath = self.runTaskPath
                runTask.arguments = ["-p", self.analyzeFilesPath, "-r", "json:taylor_report.json"]
                runTask.launch()
                runTask.waitUntilExit()
                let reporterPath = self.analyzeFilesPath.stringByAppendingPathComponent("taylor_report.json")
                expect(ReporterComparator().compareReporters(self.resultFilePath, secondReporterPath: reporterPath)).to(beTrue())
                do {
                    if NSFileManager.defaultManager().fileExistsAtPath(reporterPath) {
                        try NSFileManager.defaultManager().removeItemAtPath(reporterPath)
                    }
                } catch let error { print(error) }
{'fraction_non_alphanumeric': 0.08441913938464908, 'fraction_numerical': 0.00197087484944706, 'mean_word_length': 2.55962587685113, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4549492', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2277, 'n_words': 415}