class ScanningTestCase(TestCase):
    def setUp(self):
        self.t = DirectoryScanner()

    def test_scanner_can_cope_with_scanning_a_bad_path(self):
        results = self.t.performScan(utils.bad_app_path())
        self.assertEqual(0, count_of(results))

    def test_scanner_with_crap_dir_expressions(self):
        self.t.dir_excludes += "this won't \\(compile - no way"
        self.assertRaises(re.error,
                          lambda: self.t.performScan(utils.good_app_path()))

    def test_scanner_with_crap_file_expressions(self):
        self.t.file_excludes += "this won't \\(compile - no way"
        self.assertRaises(re.error,
                          lambda: self.t.performScan(utils.good_app_path()))

    def test_scan_can_run_on_an_existing_path_and_find_some_files(self):
        count = count_of(self.t.performScan(utils.good_app_path()))
        logger.info("text edit.app contains {0} items".format(count))
        self.assertNotEqual(count, 0,
                            "should not be None, text edit exists right?")

    def test_scan_with_exclusion_of_everything(self):
        self.t.dir_excludes.append(r'.*')
        self.t.file_excludes.append(r'.*')
        count = count_of(self.t.performScan(utils.good_app_path()))
        self.assertEqual(count, 0)

    @unittest.skipIf(
        Platform.isWindows,
        "testing exclusion of .DS_Store items makes no sense on Win32")
    def test_exclusion_of_dsstore_items(self):
        results = self.t.performScan(utils.good_app_path())
        logger.info("scanning for .DS_Store... hope we dont find it")
        self.assertFalse('.DS_Store' in [file.basename for file in results])

    def test_scan_with_exclusion_of_a_file(self):
        # scan the directory and store the number of files found there
        count_of_all = count_of(DirectoryScanner().performScan(
            utils.good_app_path()))
        self.assertTrue(count_of_all > 0)
        if Platform.isMac:
            self.t.file_excludes.append(
                r'.*/TextEdit.app/Contents/Info.plist$')
        else:
            self.t.file_excludes.append(r'.*python.exe$')
        count = count_of(self.t.performScan(utils.good_app_path()))
        self.assertEqual(count, count_of_all - 1)
class MacTestBuildTreeModel(unittest.TestCase):
	"""
	Tests how a tree model is built in different path/scan situations, and with different input data - esp. between
	the mac and windows implementations.
	"""
	def setUp(self):
		self.t = DirectoryScanner()
		self.builder = DirectoryTreeBuilder()

		# construct a known directory structure, suitable for testing - it includes files, directories, ACL's, etc

		self.contents_path = self.builder.make_dir("BB/TextEdit.app/Contents", 0755)
		self.builder.make_dir("BB/TextEdit.app/Contents/Resources", 0755)
		self.builder.make_dir("BB/TextEdit.app/Contents/Frameworks", 0755)
		self.builder.create_file("BB/TextEdit.app/Contents/Stupid.txt", 0755, 425)

		# produce a single scan of the fake file-system entries
		self.p = PersistentScanningState("tree-tests.sqlite")

		self.t.addPathsForScanning([self.builder.rootDir])
		self.initialScan()
		self.mergeScan()

	def initialScan(self):
		for value in self.p.storeFilesystemSnapshot(self.t.performScan()):
			pass
		self.assertTrue(self.p.numberOfScannedFiles() > 0)

	def mergeScan(self):
		for value in self.p.storeSecondScan(self.t.performScan()):
			pass
		self.assertTrue(self.p.numberOfMergedFiles() > 0)

	def test_simple_tree_model(self):
		# grab a fake scan of something we know the content of, e.g. one directory containing one file
		builder = FileSystemTreeModelBuilder(self.p)
		builder.buildModel()

		topLevel = builder.itemRootedAtPath(self.builder.rootDir)

		itemBB = builder.childOfItem(topLevel, "BB")
		self.assertTrue(itemBB is not None)
		self.assertEqual(itemBB.data().toString(), "BB")
		self.assertEqual(itemBB.rowCount(), 1)

		# fetch something that doesn't exist - should bring me back None
		self.assertEqual(None, builder.childOfItem(itemBB, "Not Here"))
		self.assertEqual(None, builder.itemRootedAtPath(os.path.join(self.builder.rootDir, "WOOT")))

		itemTextEdit = builder.childOfItem(itemBB, "TextEdit.app")
		self.assertTrue(itemTextEdit is not None)
		self.assertEqual(itemTextEdit.data().toString(), "TextEdit.app")
		self.assertEqual(itemTextEdit.rowCount(), 1)

		itemContents = builder.childOfItem(itemTextEdit, "Contents")
		self.assertTrue(itemContents is not None)
		self.assertEqual(itemContents.data().toString(), "Contents")
		self.assertEqual(itemContents.rowCount(), 2)

		# go grab both - they should be the frameworks and resources
		items = builder.childrenOfItem(itemContents, ["Resources", "Frameworks"])
		self.assertEqual(items["Resources"].data().toString(), "Resources")
		self.assertEqual(items["Frameworks"].data().toString(), "Frameworks")