from __future__ import print_function from TestTriggers import TestTriggers import unittest import time, os, sys import P4 def connect(func): def func_wrapper(self): self.p4.connect() self.assertTrue(self.p4.connected(), "Not connected") self._setClient() func(self) self.p4.disconnect() return func_wrapper class TestCheckCaseTrigger(TestTriggers): baseTrigger = "P4Triggers.py" caseTrigger = "CheckCaseTrigger.py" def setUp(self): TestTriggers.setUp(self) self.installTrigger() def installTrigger(self): self.assertTrue(os.path.exists(self.baseTrigger), "Can't find {0}".format(self.baseTrigger)) self.assertTrue(os.path.exists(self.caseTrigger), "Can't find {0}".format(self.caseTrigger)) with open(self.caseTrigger) as f: caseTriggerCode = f.read() with open(self.baseTrigger) as f: baseTriggerCode = f.read() self.p4.connect() self._setClient() baseTriggerPath = os.path.join(self.client_root, self.baseTrigger) with open(baseTriggerPath, "w") as f: f.write(baseTriggerCode) caseTriggerPath = os.path.join(self.client_root, self.caseTrigger) with open(caseTriggerPath, "w") as f: f.write(caseTriggerCode) portWithQuotes = self.p4.port.replace("\"","") caseTrigger = 'CheckCase change-submit //... ' \ '"{0} {1} %change% port=%quote%{2}%quote%"'.format(sys.executable, caseTriggerPath, portWithQuotes) triggers = self.p4.fetch_triggers() triggers._triggers = [ caseTrigger ] self.p4.save_triggers(triggers) self.p4.disconnect() def addFile(self, name): os.makedirs(self.client_root, exist_ok=True) path, filename = os.path.split(name) if path: pname = os.path.join(self.client_root, path) os.makedirs(pname, exist_ok=True) fname = os.path.join(self.client_root, name) with open(fname, "w") as f: f.write("Content") self.p4.run_add(fname) def createFiles(self, testDir): testAbsoluteDir = os.path.join(self.client_root, testDir) os.mkdir(testAbsoluteDir) # create a bunch of files files = ('foo.txt', 'bar.txt', 'baz.txt') for file in files: fname = os.path.join(testAbsoluteDir, file) f = open(fname, "w") f.write("Test Text") f.close() self.p4.run_add(testDir + "/" + file) self.assertEqual(len(self.p4.run_opened()), len(files), "Unexpected number of open files") return files def expectFailSubmit(self, msg, desc): """Submits the changes, but expects to fail the trigger""" try: result = self.p4.run_submit("-d", desc) self.fail(msg) except P4.P4Exception as inst: pass # expected def expectSucceedSubmit(self, msg, desc): """Submits the change, but expects to succeed""" change = self.p4.fetch_change() change._description = desc self._doSubmit(msg, change) @connect def testSimpleSubmit(self): self.assertEqual(len(self.p4.run_opened()), 0, "Shouldn't have open files") testDir = 'test_files' files = self.createFiles(testDir) self.expectSucceedSubmit("Failed to submit the add", "My Add Test") @connect def testSimpleFail(self): self.addFile("foo") self.expectSucceedSubmit("Adding one file should not fail", "Simple Add") self.addFile("Foo") self.expectFailSubmit("Did not catch foo != Foo", "Foo") @connect def testDirectoryFail(self): self.addFile("foo/bar") self.expectSucceedSubmit("Adding one file should not fail", "Simple Add") self.addFile("FOO/bar") self.expectFailSubmit("Did not catch foo != FOO/bar", "bar") @connect def testRenameFile(self): self.addFile("foo") self.expectSucceedSubmit("Adding one file should not fail", "Simple Add") self.p4.run_edit("foo") self.p4.run_move("foo", "bar") self.expectSucceedSubmit("Renaming a file should not fail", "Simple Move") @connect def testRenameFail(self): self.addFile("foo") self.addFile("bar") self.expectSucceedSubmit("Adding two files should not fail", "Double Add") self.p4.run_edit("foo") self.p4.run_move("foo", "BAR") self.expectFailSubmit("Moving into a file with existing naming conflict should not succeed", "Illegal move") @connect def testDirectoryConflictFail(self): self.addFile("foo") self.expectSucceedSubmit("Adding single file should not fail", "Simple Add") self.addFile("FOO/bar") self.expectFailSubmit("Adding a directory conflicting with an existing file should not succeed", "Illegal add") @connect def testFileWithDirectoryConflictFail(self): self.addFile("FOO/bar") self.expectSucceedSubmit("Adding single file should not fail", "Simple Add") self.addFile("foo") self.expectFailSubmit("Adding a file conflicting with an existing directory should not succeed", "Illegal add") @connect def testFileWithDirectorySameChangeFail(self): self.addFile("FOO/bar") self.addFile("foo") self.expectFailSubmit("Adding a file conflicting with an existing directory should not succeed", "Illegal add") @connect def testMultipleFilesFail(self): self.addFile("foo") self.addFile("FOO") self.expectFailSubmit("Adding two conflicting files should not succeed", "Illegal add") @connect def testMultipleDirectoriesFail(self): self.addFile("foo/foo") self.addFile("FOO/foo") self.expectFailSubmit("Adding two conflicting directories should not succeed", "Illegal add") @connect def testMessageFail(self): self.addFile("foo") self.addFile("Foo") try: self.p4.run_submit('-d','Expect to fail') self.fail("Illegal add did not cause exception") except P4.P4Exception as exc: error = exc.errors[0] self.assertTrue('//depot/foo' in error) self.assertTrue('//depot/Foo' in error) if __name__ == '__main__': unittest.main()