Ejemplo n.º 1
0
 public void HelixUtil_RemoveRootFromPath()
 {
     Assert.AreEqual(@"test", HelixUtil.RemoveRootFromPath(Util.Path(@"c:\test"), @"c:"));
     Assert.AreEqual(@"test", HelixUtil.RemoveRootFromPath(Util.Path(@"c:\test"), Util.Path(@"c:\")));
     Assert.AreEqual(Util.Path(@"aa\bb"), HelixUtil.RemoveRootFromPath(Util.Path(@"aa\bb"), @""));
     Assert.AreEqual(@"bb", HelixUtil.RemoveRootFromPath(Util.Path(@"aa\bb"), @"aa"));
 }
Ejemplo n.º 2
0
        public void HelixUtil_GetExactPathName()
        {
            try
            {
                Util.Remove("AA");
                Directory.CreateDirectory("AA");
                Directory.CreateDirectory(@"AA\A1");
                File.WriteAllText(@"AA\A1\A2", "xx");
                File.WriteAllText(@"AA\AA", "aa");
                File.WriteAllText(@"AA\ABCDEFGHIJKLMNOP", "Long File Name");

                if (File.Exists(@"aa\ABCDEF~1")) //only tests on certain file systems with DOS 8.3 name support
                {
                    var exactPath = HelixUtil.GetExactPathName(@"aa\ABCDEF~1");
                    Assert.AreEqual(@"AA\ABCDEFGHIJKLMNOP", exactPath);
                }

                {
                    var exactPath = HelixUtil.GetExactPathName(@"aa\a1\a2");
                    Assert.AreEqual(@"AA\A1\A2", exactPath);
                }
                {
                    var exactPath = HelixUtil.GetExactPathName(@"aa\aa");
                    Assert.AreEqual(@"AA\AA", exactPath);
                }
            }
            finally
            {
                Util.Remove("AA");
            }
        }
Ejemplo n.º 3
0
        protected FSEntry(string fullPath, FSDirectory parent, bool whatIf)
        {
            if (string.IsNullOrEmpty(fullPath))
            {
                throw new ArgumentNullException(nameof(fullPath));
            }

            fullPath = HelixUtil.PathUniversal(fullPath);

            if (!Path.IsPathRooted(fullPath))
            {
                throw new ArgumentException("path must be rooted (ie start with c:\\)", nameof(fullPath));
            }


            if (parent != null && !fullPath.StartsWith(parent.FullName + HelixUtil.UniversalDirectorySeparatorChar))
            {
                throw new ArgumentException($"path must be a child of the parent (fullPath: {fullPath}, parent: {parent.FullName}", fullPath);
            }

            FullName = fullPath;
            Parent   = parent;
            Root     = parent?.Root ?? (this as FSDirectory);
            WhatIf   = whatIf;
        }
Ejemplo n.º 4
0
        public void HelixUtil_QuoteUnquote(string val)
        {
            if (val == null)
            {
                return;
            }

            Assert.AreEqual(val, HelixUtil.Unquote(HelixUtil.Quote(val)));
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Deletes the file
 /// </summary>
 public void Delete()
 {
     if (!WhatIf)
     {
         File.Delete(HelixUtil.PathNative(this.FullName));
     }
     ((IFSDirectoryCore)Parent).Remove(this);
     this.Exists = false;
 }
Ejemplo n.º 6
0
        public void FindChanges_AddsInOrderParentToChildren()
        {
            Decr1.UpdateTo("file1.txt < aa", "zz/file2.txt");

            using (var pair = DirectoryPair.Open(Decr1.DirectoryPath, Encr1.DirectoryPath, DerivedBytesProvider.FromPassword("password"), true, HelixFileVersion.UnitTest))
            {
                for (int i = 0; i < 100; i++)
                {
                    var changes     = pair.FindChanges();
                    var parentIndex = changes.FindIndex(c => c.DecrFileName == "zz");
                    var childIndex  = changes.FindIndex(c => HelixUtil.PathNative(c.DecrFileName) == HelixUtil.PathNative("zz/file2.txt"));
                    Assert.IsTrue(parentIndex < childIndex, "Parent Directory Add did not come before Child File Add");
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Returns a path relative to the root, in native format
        /// </summary>
        public string PathRelative(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            path = HelixUtil.PathUniversal(path);

            if (Path.IsPathRooted(path))
            {
                path = RemoveRootFromPath(path, FullName);
            }
            return(path);
        }
Ejemplo n.º 8
0
        public void DirectoryPair_RandomizedInitialization()
        {
            RandomValue.NewTest(1836678571);

            for (int i = 0; i < 10; i++)
            {
                var dirStruct = RandomValue.GetDirectoryStructure(10)
                                .Where(d => HelixUtil.IsValidPath(d))
                                .ToArray();

                ResetDirectory();

                var setupItems = RandomValue.ChooseMany(dirStruct);
                foreach (var item in setupItems)
                {
                    if (item.EndsWith(Path.DirectorySeparatorChar.ToString()))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine("1-Orig", item + "a")));
                    }
                    else
                    {
                        var fileName = Path.Combine("1-Orig", item);
                        //Creates parent directory
                        Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine("1-Orig", item + "a")));
                        File.WriteAllText(fileName, RandomValue.GetValue <string>());
                    }
                }

                string password = RandomValue.GetValue <string>() ?? "";
                using (var origToEncr = DirectoryPair.Open("2-Encr", "1-Orig", DerivedBytesProvider.FromPassword(password), true, HelixFileVersion.UnitTest))
                    using (var encrToDecr = DirectoryPair.Open("2-Encr", "3-Decr", DerivedBytesProvider.FromPassword(password), true, HelixFileVersion.UnitTest))
                    {
                        //Orig => Encr
                        origToEncr.SyncChanges();
                        Assert.AreEqual(0, origToEncr.FindChanges().Count);


                        //Encr => Decr
                        encrToDecr.SyncChanges();
                        Assert.AreEqual(0, origToEncr.FindChanges().Count);
                    }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Moves or renames the file.
        /// </summary>
        /// <param name="destinationPath">Should be an absolute path or relitive to the root</param>
        public FSFile MoveTo(string destinationPath)
        {
            if (string.IsNullOrEmpty(destinationPath))
            {
                throw new ArgumentNullException(nameof(destinationPath));
            }

            destinationPath = HelixUtil.PathUniversal(destinationPath);

            if (Path.IsPathRooted(destinationPath))
            {
                destinationPath = RemoveRootFromPath(destinationPath, Root.FullName);
            }

            var destinationDirectory = Path.GetDirectoryName(destinationPath);

            if (Root.TryGetEntry(destinationDirectory) as FSDirectory == null)
            {
                throw new System.IO.DirectoryNotFoundException("Could not find a part of the path");
            }

            if ((Root.TryGetEntry(destinationDirectory) as FSDirectory).TryGetEntry(Path.GetFileName(destinationPath)) != null)
            {
                throw new System.IO.DirectoryNotFoundException("Cannot move a file when that file already exists.");
            }

            var newEntry = new FSFile(HelixUtil.JoinUniversal(Root.FullName, destinationPath), Root.TryGetEntry(destinationDirectory) as FSDirectory, WhatIf);

            newEntry.PopulateFromInfo(this.LastWriteTimeUtc, this.Length);

            if (!WhatIf)
            {
                File.Move(HelixUtil.PathNative(this.FullName), HelixUtil.PathNative(Path.Combine(Root.FullName, destinationPath)));
            }

            ((IFSDirectoryCore)Parent).Remove(this);
            ((IFSDirectoryCore)(Root.TryGetEntry(destinationDirectory) as FSDirectory)).Add(newEntry);

            return(newEntry);
        }
Ejemplo n.º 10
0
            static async Task <(Build Build, HelixLogInfo?LogInfo, List <string> BadLogs)> SearchBuild(
                DevOpsServer server,
                DotNetQueryUtil queryUtil,
                Regex textRegex,
                Build build)
            {
                var badLogList = new List <string>();

                try
                {
                    var workItems = await queryUtil
                                    .ListHelixWorkItemsAsync(build, DotNetUtil.FailedTestOutcomes)
                                    .ConfigureAwait(false);

                    foreach (var workItem in workItems)
                    {
                        var logInfo = await HelixUtil.GetHelixLogInfoAsync(server, workItem);

                        if (logInfo.ConsoleUri is object)
                        {
                            var isMatch = await queryUtil.SearchFileForAnyMatchAsync(
                                logInfo.ConsoleUri,
                                textRegex,
                                ex => badLogList.Add($"Unable to search helix logs {build.Id} {workItem.HelixInfo.JobId}, {logInfo.ConsoleUri}: {ex.Message}")).ConfigureAwait(false);

                            if (isMatch)
                            {
                                return(build, logInfo, badLogList);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    badLogList.Add($"Unable to search helix logs for {build.Id}:  {ex.Message}");
                }

                return(build, null, badLogList);
            }
Ejemplo n.º 11
0
        public async Task <List <HelixWorkItemRestInfo> > FailedWorkItems(string project, int buildNumber, [FromQuery] bool failed = false)
        {
            if (!failed)
            {
                throw new Exception("Not supported");
            }

            var queryUtil = await QueryUtilFactory.CreateDotNetQueryUtilForUserAsync();

            var build = await queryUtil.Server.GetBuildAsync(project, buildNumber);

            var workItems = await queryUtil.ListHelixWorkItemsAsync(build, DotNetUtil.FailedTestOutcomes);

            var list = new List <HelixWorkItemRestInfo>();

            foreach (var workItem in workItems)
            {
                var restWorkItem = new HelixWorkItemRestInfo();
                restWorkItem.Job      = workItem.JobId;
                restWorkItem.WorkItem = workItem.WorkItemName;

                var logs    = new List <HelixLogRestInfo>();
                var logInfo = await HelixUtil.GetHelixLogInfoAsync(queryUtil.Server, workItem);

                foreach (var entry in logInfo.GetUris())
                {
                    logs.Add(new HelixLogRestInfo()
                    {
                        Name = entry.kind.ToString(),
                        Uri  = entry.Uri,
                    });
                }

                restWorkItem.Logs = logs.ToArray();
                list.Add(restWorkItem);
            }

            return(list);
        }
Ejemplo n.º 12
0
        public void DirectoryPair_DecrToEncr()
        {
            if (Directory.Exists("Orig"))
            {
                Directory.Delete("Orig", true);
            }
            if (Directory.Exists("Decr"))
            {
                Directory.Delete("Decr", true);
            }
            if (Directory.Exists("Encr"))
            {
                Directory.Delete("Encr", true);
            }

            Directory.CreateDirectory("Orig");
            Directory.CreateDirectory("Encr");
            Directory.CreateDirectory("Decr");

            //New (Orig => Encr)
            Util.WriteTextFile("Orig/test.txt", "hello world");
            using (var origToEncr = DirectoryPair.Open("Orig", "Encr", DerivedBytesProvider.FromPassword("password"), true, HelixFileVersion.UnitTest))
                using (var encrToDecr = DirectoryPair.Open("Decr", "Encr", DerivedBytesProvider.FromPassword("password"), true, HelixFileVersion.UnitTest))
                {
                    var changes = origToEncr.FindChanges(clearCache: false);
                    Assert.AreEqual(1, changes.Count);
                    Assert.AreEqual("test.txt", changes[0].DecrFileName);

                    Assert.AreEqual(SyncStatus.Success, origToEncr.TrySync(changes[0]).SyncStatus);

                    changes = origToEncr.FindChanges(clearCache: true);
                    Assert.IsTrue(0 == changes.Count, "Single file sync still contains changes");

                    //New (Encr => Decr)
                    changes = encrToDecr.FindChanges(clearCache: true);
                    Assert.AreEqual(1, changes.Count);
                    Assert.IsTrue(changes[0].SyncMode == PreSyncMode.EncryptedSide);
                    Assert.IsTrue(changes[0].EncrFileName.EndsWith(".hx"));

                    Assert.AreEqual(SyncStatus.Success, encrToDecr.TrySync(changes[0]).SyncStatus);

                    Assert.AreEqual(0, encrToDecr.FindChanges(clearCache: true).Count);

                    Assert.AreEqual(HelixUtil.TruncateTicks(File.GetLastWriteTimeUtc(Util.Path("Orig/test.txt"))), HelixUtil.TruncateTicks(File.GetLastWriteTimeUtc(Util.Path("Decr/test.txt"))));
                    Assert.AreEqual("hello world", File.ReadAllText(Path.Combine("Decr", "test.txt")));


                    //Add (Orig => Encr)
                    origToEncr.ClearCache();
                    Util.WriteTextFile("Orig/test2.txt", "aa");
                    changes = origToEncr.FindChanges(clearCache: true);
                    Assert.AreEqual(1, changes.Count);
                    Assert.IsTrue(changes[0].SyncMode == PreSyncMode.DecryptedSide);
                    Assert.AreEqual("test2.txt", changes[0].DecrFileName);

                    Assert.AreEqual(SyncStatus.Success, origToEncr.TrySync(changes[0]).SyncStatus);

                    Assert.AreEqual(0, origToEncr.FindChanges(clearCache: true).Count);

                    //Add (Encr => Decr)
                    encrToDecr.ClearCache();
                    changes = encrToDecr.FindChanges(clearCache: true);
                    Assert.AreEqual(1, changes.Count);
                    Assert.IsTrue(changes[0].SyncMode == PreSyncMode.EncryptedSide);
                    Assert.IsTrue(changes[0].EncrFileName.EndsWith(".hx"));

                    Assert.AreEqual(SyncStatus.Success, encrToDecr.TrySync(changes[0]).SyncStatus);

                    Assert.AreEqual(0, encrToDecr.FindChanges().Count);

                    Assert.AreEqual(HelixUtil.TruncateTicks(File.GetLastWriteTimeUtc(Util.Path("Orig/test2.txt"))),
                                    HelixUtil.TruncateTicks(File.GetLastWriteTimeUtc(Util.Path(@"Decr/test2.txt"))));
                    Assert.AreEqual("aa", File.ReadAllText(Util.Path("Decr/test2.txt")));



                    //System.Threading.Thread.Sleep(timeStampPrecision); //ensure the timestap changes

                    //Update (Orig => Encr)
                    origToEncr.ClearCache();
                    Util.WriteTextFile("Orig/test.txt", "hello world2");
                    changes = origToEncr.FindChanges();
                    Assert.AreEqual(1, changes.Count);
                    Assert.IsTrue(changes[0].SyncMode == PreSyncMode.DecryptedSide);
                    Assert.AreEqual("test.txt", changes[0].DecrFileName);

                    Assert.AreEqual(SyncStatus.Success, origToEncr.TrySync(changes[0]).SyncStatus);

                    Assert.AreEqual(0, origToEncr.FindChanges().Count);

                    //Update (Encr => Decr)
                    encrToDecr.ClearCache();
                    changes = encrToDecr.FindChanges();
                    Assert.AreEqual(1, changes.Count);
                    Assert.IsTrue(changes[0].SyncMode == PreSyncMode.EncryptedSide);
                    Assert.IsTrue(changes[0].EncrFileName.EndsWith(".hx"));

                    Assert.AreEqual(SyncStatus.Success, encrToDecr.TrySync(changes[0]).SyncStatus);

                    Assert.AreEqual(0, encrToDecr.FindChanges().Count);
                    Assert.AreEqual(
                        HelixUtil.TruncateTicks(File.GetLastWriteTimeUtc(Util.Path("Orig/test.txt"))),
                        HelixUtil.TruncateTicks(File.GetLastWriteTimeUtc(Util.Path("Decr/test.txt"))));



                    Assert.AreEqual("hello world2", File.ReadAllText(Util.Path("Decr/test.txt")));

                    //Delete (Orig => Encr)
                    origToEncr.ClearCache();
                    File.Delete(Util.Path("Orig/test.txt"));
                    changes = origToEncr.FindChanges();
                    Assert.AreEqual(1, changes.Count);
                    Assert.IsTrue(changes[0].SyncMode == PreSyncMode.DecryptedSide);
                    Assert.AreEqual("test.txt", changes[0].DecrFileName);

                    Assert.AreEqual(SyncStatus.Success, origToEncr.TrySync(changes[0]).SyncStatus);
                    Assert.IsTrue(origToEncr.SyncLog.FindByDecrFileName("test.txt").EntryType == FileEntryType.Removed);
                    Assert.AreEqual(0, origToEncr.FindChanges().Count);


                    //Delete (Encr => Decr)
                    encrToDecr.ClearCache();
                    changes = encrToDecr.FindChanges();
                    Assert.IsTrue(1 == changes.Count, "Delete change did not propigate correctly");
                    Assert.IsTrue(changes[0].SyncMode == PreSyncMode.EncryptedSide);
                    Assert.IsTrue(changes[0].EncrFileName.EndsWith(".hx"));

                    Assert.AreEqual(SyncStatus.Success, encrToDecr.TrySync(changes[0]).SyncStatus);

                    Assert.IsFalse(File.Exists(Util.Path("Decr/test.txt")), "Missing file Decr/test.txt");
                }
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Returns if the file or directory exists.
 /// </summary>
 /// <param name="path">Path can be relative to the directory or absolute</param>
 public bool ChildExists(string path)
 {
     path = HelixUtil.PathUniversal(path);
     return(TryGetEntry(path) != null);
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Forces an entry to be refreshed based of the Filesystem
        /// </summary>
        public void RefreshEntry(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            this.Load();


            path = HelixUtil.PathUniversal(path);

            if (Path.IsPathRooted(path))
            {
                path = RemoveRootFromPath(path, FullName);
            }

            var     split = path.Split(HelixUtil.UniversalDirectorySeparatorChar);
            FSEntry newEntry;

            FileSystemInfo info;
            string         fullPath = HelixUtil.JoinUniversal(this.FullName, split[0]);

            if (System.IO.Directory.Exists(fullPath))
            {
                info = new DirectoryInfo(fullPath);
            }
            else if (System.IO.File.Exists(fullPath))
            {
                info = new FileInfo(fullPath);
            }
            else
            {
                info = null;
            }

            if (info is DirectoryInfo dirInfo)
            {
                newEntry = new FSDirectory(dirInfo, this, this.WhatIf, isRoot: false);
            }
            else if (info is FileInfo fileInfo)
            {
                newEntry = new FSFile(fileInfo, this, this.WhatIf);
            }
            else
            {
                newEntry = null; //not found
            }

            children.TryGetValue(split[0], out FSEntry oldEntry);
            if (newEntry?.EntryType != oldEntry?.EntryType)
            {
                if (oldEntry != null)
                {
                    children.Remove(oldEntry);
                }
                if (newEntry != null)
                {
                    children.Add(newEntry);
                }
            }
            else if (newEntry != null && oldEntry != null)
            {
                oldEntry.PopulateFromInfo(newEntry.LastWriteTimeUtc, newEntry.Length);
            }
            else if (newEntry != null)
            {
                children.Add(newEntry);
                if (split.Length > 1)
                {
                    (newEntry as FSDirectory)?.RefreshEntry(HelixUtil.JoinUniversal(split.Skip(1).ToArray()));
                }
            }
        }