// // Copyright 2014 Perforce Software Inc. // using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Perforce.Helper; using Perforce.P4; using System.IO; using System.Collections.Generic; using Perforce; namespace Perforce_test { [TestClass] public class PerforceHelperTest { private static string serverUri = "localhost:1666"; private static string username = "bruno"; private static string password = "Brunopass"; private static string badPassword = "asdfsadfaf"; private static string dirPath = "//depot"; private static string filesPath = "//depot/Jam/MAIN/src"; private static string viewDepotPath = "//depot/Jam/MAIN/..."; private static string editFileDepotPath = "//depot/Jam/MAIN/src/command.c"; private static string graphicFileDepotPath = "//depot/www/dev/images/jamgraph-jam.gif"; private static string fileHistoryDepotPath = "//depot/Jam/REL2.1/src/headers.c"; private static string rollbackDepotPath = "//depot/Jam/MAIN/src/lists.c"; private static string clientName = "TEST_CLIENT"; private static string clientDescription = "Client created for test harness"; private static string clientRoot = @"c:\ws\TEST_CLIENT"; private static string clientFileBase = clientRoot + @"\depot\Jam\MAIN\src\"; private static string editFileClientPath = clientFileBase + "command.c"; private PerforceHelper helper; public TestContext TestContext { get; set; } [TestInitialize] public void Setup() { Console.WriteLine("### Setup()"); helper = new PerforceHelper(serverUri, username); if (TestContext.Properties.Contains("init")) { var initValue = (string) TestContext.Properties["init"]; if (initValue.Contains("login")) { Console.WriteLine("--- setup: logging in"); helper.Login(password); } if (initValue.Contains("client")) { Console.WriteLine("--- setup: creating client"); var client = helper.CreateClient(clientName, clientRoot, clientDescription); } if (initValue.Contains("mapping")) { Console.WriteLine("--- setup: creating client mapping"); helper.IncludeInClient(viewDepotPath); } } Console.WriteLine("### TEST: " + TestContext.TestName); } [TestMethod] public void T001_ConnectionTest() { Console.WriteLine("--- trying to connect"); Assert.IsTrue(helper.Connect()); Console.WriteLine("--- checking connection"); Assert.IsTrue(helper.IsConnected()); } [TestMethod] public void T002_ServerTimeTest() { var currentTime = Utility.GetEpochTime(); Console.WriteLine("EPOCH TIME: {0}", currentTime); var datetime = helper.GetServerTime(); Console.WriteLine(datetime); } [TestMethod] public void T004_LoginTest() { Console.WriteLine("--- logging out to begin test"); helper.Logout(); Console.WriteLine("--- checking with bad password"); Assert.IsFalse(helper.Login(badPassword).Item1); Assert.IsFalse(helper.IsLoggedIn()); Console.WriteLine("--- checking with valid password"); Assert.IsTrue(helper.Login(password).Item1); Assert.IsTrue(helper.IsLoggedIn()); } [TestMethod] [TestProperty("init", "login")] public void T006_GetKeyTest() { var key = "p4search.url"; var result = helper.GetKey(key); Console.WriteLine("{0} = {1}", key, result); } [TestMethod] [TestProperty("init", "login")] public void T100_ListDepotsTest() { Console.WriteLine("--- getting list of local depots"); var depots = helper.ListDepots(true); Assert.IsNotNull(depots); foreach (var d in depots) { Console.WriteLine(d.Id); } } [TestMethod] [TestProperty("init", "login")] public void T101_ListDirectoriesTest() { Console.WriteLine("--- getting list of directories in {0}", dirPath); var dirs = helper.ListDirectories(dirPath); foreach (var d in dirs) { Console.WriteLine(d); } Console.WriteLine("dirPath = " + dirPath); } [TestMethod] [TestProperty("init", "login")] public void T102_ListFilesTest() { var fileMetadataList = helper.ListFiles(filesPath + "/*"); Console.WriteLine("--- listing files on path"); foreach (var md in fileMetadataList) { Console.WriteLine(md.GetFileName()); } } [TestMethod] [TestProperty("init", "login")] public void T103_ListClientsTest() { var clientList = helper.ListClients(); Console.WriteLine("--- listing all clients owned by {0}", username); foreach (var c in clientList) { Console.WriteLine(c.Name); } } [TestMethod] [TestProperty("init", "login")] public void T104_CreateClientTest() { Console.WriteLine("--- creating a client {0}", clientName); var client = helper.CreateClient(clientName, clientRoot, clientDescription); Assert.IsTrue(helper.ClientExists(clientName)); } [TestMethod] [TestProperty("init", "login,client")] public void T105_IncludeInClientTest() { var client = helper.IncludeInClient(viewDepotPath); var entry = helper.CreateMapEntry(viewDepotPath, client.Name, MapType.Include); Assert.IsTrue(client.ViewMap.Contains(entry)); } [TestMethod] [TestProperty("init", "login,client")] public void T106_RemoveFromClientTest() { var client = helper.RemoveFromClient(viewDepotPath); if(client != null && client.ViewMap != null) { var entry = helper.CreateMapEntry(viewDepotPath, client.Name, MapType.Include); Assert.IsFalse(client.ViewMap.Contains(entry)); } } [TestMethod] [TestProperty("init", "login,client")] public void T108_CreateChangelistTest() { Console.WriteLine("--- creating a new changelist"); var change = helper.CreateChangelist("a test changelist"); Console.WriteLine("--- checking to see if changelist {0} is pending", change.Id); Assert.IsTrue(change.Pending); Console.WriteLine("--- retrieving the changelist from Perforce"); var change2 = helper.GetChangelist(change.Id); Assert.AreEqual(change2.Id, change.Id); Console.WriteLine("--- deleting the pending changelist"); helper.DeleteChangelist(change.Id); } [TestMethod] [TestProperty("init", "login,client")] public void T109_GetCurrentPendingChangelistTest() { var change = helper.CreateChangelist("a test pending changelist"); Console.WriteLine("--- trying to get the current pending changelist"); var change2 = helper.GetCurrentPendingChangelist(); Console.WriteLine("--- testing to verify that changelist was retrieved"); Assert.IsNotNull(change2); Console.WriteLine("Current pending changelist is {0}", change2.Id); } [TestMethod] [TestProperty("init", "login,client")] public void T110_GetChangelists() { var changes = helper.ListChanges(filesPath); foreach (var c in changes) { Console.WriteLine("--- {0}", c.Id); } } [TestMethod] [TestProperty("init", "login,client")] public void T112_GetAllPendingChangelists() { var c1 = helper.CreateChangelist("test change1"); var c2 = helper.CreateChangelist("test change2"); var changelists = helper.GetAllPendingChangelists(); Assert.AreEqual(2, changelists.Count); helper.DeleteChangelist(c1); helper.DeleteChangelist(c2); } [TestMethod] [TestProperty("init", "login,client")] public void T200_SyncWorkspaceFilesTest() { var files = helper.SyncWorkspaceFiles(force:true); if (files != null) { Console.WriteLine("--- sync results:"); foreach (var f in files) { Console.WriteLine(f.DepotPath); } } else { Console.WriteLine("--- no files need synchronization"); } } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T201_GetFileMetaDataTest() { var paths = new List(); paths.Add(editFileDepotPath); paths.Add(editFileClientPath); var results = helper.GetFileMetaData(paths); foreach (var md in results) { Console.WriteLine(md.DepotPath.Path); } Console.WriteLine("-- testing metadata for new file"); var f = createRandomFile(clientFileBase); var md2 = helper.GetFileMetaData(f); Assert.IsNull(md2); helper.MarkFileForAdd(f); md2 = helper.GetFileMetaData(f); Assert.IsNotNull(md2); Console.WriteLine(md2.DepotPath.Path); var change = helper.GetCurrentPendingChangelist(); helper.RevertChangelist(change); helper.DeleteChangelist(change); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T203_GetFilesModifiedSinceTest() { var serverTime = helper.GetServerTime(); Console.WriteLine("SERVER TIME: {0}", serverTime); var files = helper.ListFiles("//...", showDeleted: true, sinceTime: serverTime); Assert.IsNull(files); var f = createRandomFile(clientFileBase); helper.MarkFileForAdd(f); helper.SubmitChangelist(); files = helper.ListFiles("//...", showDeleted: true, sinceTime: serverTime); Assert.AreEqual(1, files.Count); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T204_MoveFilesToNewChangelistTest() { helper.RevertFiles(serverOnly: false, paths: "//..."); helper.SyncWorkspaceFiles(); var editList = helper.CheckoutFiles(false, filesPath + "/..."); var count = editList.Count; var currentChange = helper.GetCurrentPendingChangelist(); Console.WriteLine("--- current changelist has {0} files", currentChange.Files.Count); Assert.AreEqual(count, currentChange.Files.Count); var newChange = helper.CreateChangelist("a TEST changelist"); Assert.AreEqual(0, newChange.Files.Count); var files = new List(); files.Add(editFileDepotPath); Console.WriteLine("--- moving files to changelist {0}", newChange.Id); helper.MoveFilesToNewChangelist(files, newChange.Id); currentChange = helper.GetChangelist(currentChange.Id); Console.WriteLine("--- changelist now has {0} files", currentChange.Files.Count); Assert.AreEqual(count - 1, currentChange.Files.Count); newChange = helper.GetChangelist(newChange.Id); Assert.AreEqual(1, newChange.Files.Count); // CLEAN UP helper.RevertChangelist(currentChange); helper.DeleteChangelist(currentChange); helper.RevertChangelist(newChange); helper.DeleteChangelist(newChange); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T210_AddFileTest() { var f = createRandomFile(clientFileBase); helper.MarkFileForAdd(f); var list = helper.GetChangelistFiles(); foreach (var item in list) { Console.WriteLine(item.DepotPath.Path); } helper.SubmitChangelist(); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T220_EditFileTest() { var files = helper.CheckoutFiles(false, editFileDepotPath); if (files != null) { foreach (var f in files) { Console.WriteLine(f); appendTextToFile(f.LocalPath.Path); } helper.SubmitChangelist(); } } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T222_DeleteFilesTest() { var f = createRandomFile(clientFileBase); Console.WriteLine("-- Created file: {0}", f); helper.MarkFileForAdd(f); helper.SubmitChangelist(); Console.WriteLine("-- Preparing to delete"); var results = helper.DeleteFiles(serverOnly:false, paths: f); foreach (var r in results) { Console.WriteLine("-- result: {0}", r.DepotPath.Path); } helper.SubmitChangelist(); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T224_RenameFileTest() { var f = createRandomFile(clientFileBase); Console.WriteLine("-- Source file: {0}", f); helper.MarkFileForAdd(f); helper.SubmitChangelist(); Console.WriteLine("-- added {0} to server", f); var fprime = getRandomFileName(clientFileBase); Console.WriteLine("-- Destination file: {0}", fprime); var results = helper.RenameFile(f, fprime); foreach (var r in results) { Console.WriteLine("-- result: {0}", r.DepotPath.Path); } var submitResults = helper.SubmitChangelist(); Console.WriteLine("-- submitted changelist @{0}", submitResults.Results.ChangeIdAfterSubmit); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T225_CopyFileTest() { var f1 = createRandomFile(clientFileBase); Console.WriteLine("-- Source file: {0}", f1); helper.MarkFileForAdd(f1); helper.SubmitChangelist(); var f2 = getRandomFileName(clientFileBase); helper.CopyFile(f1, f2); var submitResults = helper.SubmitChangelist(); Console.WriteLine("-- submitted changelist @{0}", submitResults.Results.ChangeIdAfterSubmit); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T226_GetFileHistoryTest() { var history = helper.GetFileHistory(fileHistoryDepotPath); foreach (var h in history) { Console.WriteLine("DepotPath: {0}", h.DepotPath.Path); Console.WriteLine("Revision: {0}", h.Revision); Console.WriteLine("Modified by: {0}", h.UserName); Console.WriteLine("Description: {0}", h.Description); Console.WriteLine("Checked in: {0}", h.Date); } } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T227_ShelveFileTest() { var f = createRandomFile(clientFileBase); Console.WriteLine("-- Source file: {0}", f); var specs = helper.MarkFileForAdd(f); Assert.AreEqual(1, specs.Count); var dp = specs[0].DepotPath.Path; Assert.IsFalse(helper.IsFileShelved(dp)); helper.ShelveFiles(dp); Assert.IsTrue(helper.IsFileShelved(dp)); var shelved = helper.GetShelvedChangelistFiles(); foreach (var s in shelved) { Console.WriteLine("-- {0}", s.Path.Path); } var results = helper.DeleteShelvedFiles(paths: "//..."); shelved = helper.GetShelvedChangelistFiles(); Assert.IsNull(shelved); helper.RevertFiles(serverOnly: true, paths: "//..."); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T228_ReconcileFileTest() { var f = createRandomFile(clientFileBase); var result = helper.ReconcileFiles(f); Console.WriteLine(result); helper.SubmitChangelist(); System.IO.File.SetAttributes(f, FileAttributes.Normal); System.IO.File.Delete(f); result = helper.ReconcileFiles(f); Console.WriteLine(result); } [TestMethod] [TestProperty("init", "login")] public void T230_GetFileFromServerTest() { var filename = helper.GetFileFromServer(graphicFileDepotPath); Console.WriteLine(filename); } [TestMethod] [TestProperty("init", "login")] public void T231_GetDirectoryFromServerTest() { var dir = @"c:\temp\junk"; Directory.CreateDirectory(dir); var results = helper.GetDirectoryFromServer(filesPath, dir); Console.WriteLine(results); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T232_GetFileFromShelfTest() { var f = createRandomFile(clientFileBase); Console.WriteLine("-- Source file: {0}", f); var specs = helper.MarkFileForAdd(f); var depotPath = specs[0].DepotPath.Path; Console.WriteLine("-- Depot path: {0}", depotPath); var shelvedFiles = helper.ShelveFiles(f); Assert.IsTrue(helper.IsFileShelved(depotPath)); var change = helper.GetOrCreatePendingChangelist(); Console.WriteLine("-- Change id: {0}", change.Id); helper.RevertFiles(serverOnly: false, paths: f); var downloaded = helper.GetFileFromShelf(depotPath); } [TestMethod] [TestProperty("init", "login,client")] public void T235_GetPathSizesTest() { var sizes = helper.GetPathSizes(viewDepotPath); Console.WriteLine("{0} {1} files {2} kb", sizes.Path, sizes.FileCount, sizes.FileSize/1024); } [TestMethod] [TestProperty("init", "login,client")] public void T240_IsDirectoryMappedTest() { var path = "//depot/MapTest"; helper.IncludeInClient(path + "/..."); var result = helper.IsDirectoryMapped(path); Assert.IsTrue(result); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T250_RollbackFileToRevisionTest() { helper.SyncWorkspaceFiles(); var md = helper.GetFileMetaData(rollbackDepotPath); Console.WriteLine("HEAD REV: {0}", md.HeadRev); var fileSpecs = helper.CheckoutFiles(serverOnly: false, paths: rollbackDepotPath); var localFile = fileSpecs[0].LocalPath; appendTextToFile(localFile.Path); helper.SubmitChangelist(); md = helper.GetFileMetaData(rollbackDepotPath); Console.WriteLine("HEAD REV: {0}", md.HeadRev); helper.RollbackFileToRevision(rollbackDepotPath, 3); md = helper.GetFileMetaData(rollbackDepotPath); Console.WriteLine("HEAD REV: {0}", md.HeadRev); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T251_RollbackFolderToChangelistTest() { helper.SyncWorkspaceFiles(); var path = "//depot/Jam/MAIN/src"; var changeId = 320; var results = helper.RollbackFolderToChangelist(path, changeId); } [TestMethod] [TestProperty("init", "login,client,mapping")] public void T260_SubmitFileTest() { var f = createRandomFile(clientFileBase); Console.WriteLine("-- Created file: {0}", f); var addResults = helper.MarkFileForAdd(f); Assert.AreEqual(addResults.Count, 1); var depotFile = addResults[0].DepotPath.Path; Console.WriteLine("-- depot file: {0}", depotFile); var submitResults = helper.SubmitSingleFile(depotFile, "TEST: SubmitFileTest()"); Console.WriteLine("-- change {0} submitted", submitResults.Results.ChangeIdAfterSubmit); } [TestMethod] [TestProperty("init", "login,client")] public void T998_DeleteClientTest() { Console.WriteLine("--- deleting pending changelist"); helper.DeletePendingChangeList(); Console.WriteLine("--- removing the client {0}", clientName); helper.DeleteClient(clientName); Assert.IsFalse(helper.ClientExists(clientName)); } [TestMethod] public void T999_LogoutTest() { Console.WriteLine("--- logging out"); Assert.IsTrue(helper.Logout()); Assert.IsFalse(helper.IsLoggedIn()); } [TestCleanup] public void Teardown() { Console.WriteLine("### Teardown()"); } // helper methods private string createRandomFile(string baseDir) { var fileName = getRandomFileName(baseDir); byte[] data = new byte[8192]; Random rng = new Random(); using (FileStream stream = System.IO.File.OpenWrite(fileName)) { for (int i = 0; i < 8; i++) { rng.NextBytes(data); stream.Write(data, 0, data.Length); } } return fileName; } private string getRandomFileName(string baseDir) { var rndName = Path.GetRandomFileName(); var fileName = baseDir + rndName; return fileName; } private void appendTextToFile(string file) { var random = new Random(); var numlines = random.Next(10)+1; using (StreamWriter sw = System.IO.File.AppendText(file)) { for (var i = 0; i < numlines; i++) { sw.WriteLine(generateRandomString()); } } } private string generateRandomString() { var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var stringChars = new char[8]; var random = new Random(); for (int i = 0; i < stringChars.Length; i++) { stringChars[i] = chars[random.Next(chars.Length)]; } return new String(stringChars); } } }