Exemple #1
0
        public static FileComparison CompareFileVersions(string file1, string file2)
        {
            if (string.IsNullOrWhiteSpace(file1))
            {
                throw new ArgumentNullException(nameof(file1));
            }
            if (string.IsNullOrWhiteSpace(file2))
            {
                throw new ArgumentNullException(nameof(file2));
            }

            FileComparison  retValue         = FileComparison.Error;
            FileVersionInfo file1VersionInfo = FileVersionInfo.GetVersionInfo(file1);
            FileVersionInfo file2VersionInfo = FileVersionInfo.GetVersionInfo(file2);

            retValue = ComparePart(file1VersionInfo.FileMajorPart,
                                   file2VersionInfo.FileMajorPart);

            if (retValue != FileComparison.Same)
            {
                retValue = ComparePart(file1VersionInfo.FileMinorPart,
                                       file2VersionInfo.FileMinorPart);
                if (retValue != FileComparison.Same)
                {
                    retValue = ComparePart(file1VersionInfo.FileBuildPart,
                                           file2VersionInfo.FileBuildPart);
                    if (retValue != FileComparison.Same)
                    {
                        retValue = ComparePart(file1VersionInfo.FilePrivatePart,
                                               file2VersionInfo.FilePrivatePart);
                    }
                }
            }
            return(retValue);
        }
        /// <summary>
        /// Uploads the file which is in the right side of the comparison.
        /// </summary>
        /// <param name="id">The comparison ID.</param>
        /// <param name="content">Binary content of the file.</param>
        /// <returns>
        /// A <see cref="Task" /> object for the result of the async task.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">content</exception>
        public async Task <FileComparisonDTO> UploadRightAsync(int id, byte[] content)
        {
            if (content == null || content.Length == 0)
            {
                throw new ArgumentNullException(nameof(content));
            }

            var files = await this.context.FilesForComparison.Where(f => f.Id == id).FirstOrDefaultAsync();

            if (files != null)
            {
                files.Right = content;
            }
            else
            {
                files = new FileComparison
                {
                    Right = content,
                    Id    = id
                };
                this.context.FilesForComparison.Add(files);
            }

            await this.context.SaveChangesAsync();

            return(AutoMapper.Mapper.Map <FileComparison, FileComparisonDTO>(files));
        }
Exemple #3
0
        private static bool IsMatch(FileComparison a, IEnumerable <FileComparison> b)
        {
            var match = b.FirstOrDefault(x => x.File == a.File);

            if (match == null)
            {
                return(false);
            }

            return(AreEqual(a.FullName, match.FullName));
        }
Exemple #4
0
        private bool ValidatePips(FrontEndContext context, PipGraph.Builder pipGraph, AbsolutePath testFolder, string specFile, string shortName, string lkgFile, bool dontValidatePipsEnabled)
        {
            bool hasLkgFile = !string.IsNullOrEmpty(lkgFile);
            var  hasPips    = pipGraph.PipTable.Keys.Count(pipId => !pipGraph.PipTable.GetPipType(pipId).IsMetaPip()) > 0;

            if (dontValidatePipsEnabled)
            {
                if (hasLkgFile)
                {
                    m_diagnosticHandler(
                        new Diagnostic(
                            0,
                            EventLevel.Error,
                            C($"Test calls 'Testing.dontValidatePips', but lkg file '{lkgFile}' is present. Either remove that call or delete the file.{GetAutoFixString()}"),
                            default(Location)));
                    return(FailOrDeleteLkg(lkgFile));
                }

                if (!hasPips)
                {
                    m_diagnosticHandler(
                        new Diagnostic(
                            0,
                            EventLevel.Error,
                            C($"Test calls 'Testing.dontValidatePips', but no pips were created. Remove that call.{GetAutoFixString()}"),
                            default(Location)));
                    return(FailOrDeleteLkg(lkgFile));
                }
            }
            else
            {
                if (!hasPips)
                {
                    if (hasLkgFile)
                    {
                        m_diagnosticHandler(
                            new Diagnostic(
                                0,
                                EventLevel.Error,
                                C($"No pips were created in the test, but lkg file '{lkgFile}' is present. Delete the file.{GetAutoFixString()}"),
                                default(Location)));
                        return(FailOrDeleteLkg(lkgFile));
                    }
                }
                else
                {
                    var printer = new TestPipPrinter(context.PathTable, context.StringTable, testFolder);
                    var actual  = printer.Print(pipGraph);

                    if (!hasLkgFile)
                    {
                        lkgFile = Path.Combine(Path.GetDirectoryName(specFile), Path.GetFileNameWithoutExtension(specFile), shortName + ".lkg");
                        m_diagnosticHandler(
                            new Diagnostic(
                                0,
                                EventLevel.Error,
                                C($"This test creates pip, but no lkgFile was encountered. Either create file '{lkgFile}' or add a call to 'Testing.dontValidatePips'.{GetAutoFixString()}"),
                                default(Location)));
                        return(FailOrUpdateLkg(lkgFile, actual));
                    }

                    if (!File.Exists(lkgFile))
                    {
                        m_diagnosticHandler(
                            new Diagnostic(
                                0,
                                EventLevel.Error,
                                C($"File '{lkgFile}' not found.{GetAutoFixString()}"),
                                default(Location)));

                        return(FailOrUpdateLkg(lkgFile, actual));
                    }

                    var    expected = TranslateLkgContentForCurrentPlatform(File.ReadAllText(lkgFile));
                    string message;
                    if (!FileComparison.ValidateContentsAreEqual(expected, actual, lkgFile, out message))
                    {
                        m_diagnosticHandler(
                            new Diagnostic(
                                0,
                                EventLevel.Error,
                                C($"Pips don't match '{lkgFile}: {message}'.{GetAutoFixString()}"),
                                default(Location)));

                        return(FailOrUpdateLkg(lkgFile, actual));
                    }
                }
            }

            return(true);
        }
Exemple #5
0
 /// <summary>
 /// Checks if <paramref name="text"/> was changed in comparison to content of file <paramref name="filepath"/>
 /// </summary>
 /// <param name="text">Text to compare with content of <paramref name="filepath"/></param>
 /// <param name="filepath">File which content wil be compared to <paramref name="text"/></param>
 /// <returns><c>true</c> if <paramref name="text"/> was changed in comparison to content of <paramref name="filepath"/>, <c>false</c> otherwsise</returns>
 bool IsChanged(String text, String filepath)
 {
     return(!(FileComparison.EqualsTextToFileText(text, filepath)));
 }
Exemple #6
0
            protected async override Task ExecuteAsync()
            {
                var folderIdentifier = GetFolderIdentifier(Key);

                var folder = await API.Folder.GetOrThrowAsync(folderIdentifier, new[]
                {
                    new PopulationDirective(nameof(FolderModel.Files))
                });


                var remoteFiles = folder.Files.Rows.Select(f => new FileComparison
                {
                    FileKey  = f.Identifier.FileKey,
                    Length   = f.Length,
                    Modified = f.Modified,
                    MD5      = f.HashMD5
                });

                var remoteFilesByKey = remoteFiles.ToDictionary(f => f.FileKey, f => f);

                Table("Remote", remoteFiles);

                var rootPath = System.IO.Path.GetFullPath(Path);

                Table("Local",
                      Directory.EnumerateFileSystemEntries(rootPath, "*.*", SearchOption.AllDirectories)
                      .Where(f => File.Exists(f))
                      .Select(f =>
                {
                    var i = new FileInfo(f);

                    var fullChangedPath   = System.IO.Path.GetFullPath(i.FullName);
                    var fullDirectoryPath = System.IO.Path.GetDirectoryName(fullChangedPath);
                    var deltaPath         = fullDirectoryPath.Length >= rootPath.Length && fullDirectoryPath.Substring(0, rootPath.Length).Equals(rootPath)
                                ? fullDirectoryPath.Substring(rootPath.Length)
                                : null;

                    if (deltaPath != null)
                    {
                        while (deltaPath.StartsWith(System.IO.Path.DirectorySeparatorChar))
                        {
                            deltaPath = deltaPath.Substring(1);
                        }

                        deltaPath = deltaPath.Replace(System.IO.Path.DirectorySeparatorChar, '/');
                    }


                    string fileKey = null;

                    if (!string.IsNullOrWhiteSpace(deltaPath))
                    {
                        var parts = deltaPath.Split().ToList();
                        parts.Add(i.Name);

                        fileKey = String.Join("/", parts.ToArray());
                    }
                    else
                    {
                        fileKey = i.Name;
                    }

                    FileComparison remote = remoteFilesByKey.ContainsKey(fileKey)
                                ? remoteFilesByKey[fileKey]
                                : null;

                    return(new
                    {
                        FileKey = fileKey,
                        i.Name,
                        Modified = i.LastWriteTimeUtc,
                        i.Length,
                        deltaPath,
                        remote = JsonConvert.SerializeObject(remote)
                    });
                })
                      );
            }