Exemple #1
0
        /// <summary>
        /// Helper method for tests of IsAnyOutOfDate.
        /// The setup required here suggests that the TargetDependencyAnalyzer
        /// class should be refactored.
        /// </summary>
        /// <param name="input1Time"></param>
        /// <param name="input2Time"></param>
        /// <param name="output1Time"></param>
        /// <param name="output2Time"></param>
        /// <param name="isUpToDate"></param>
        private void IsAnyOutOfDateTestHelper
        (
            DateTime?input1Time,
            DateTime?input2Time,
            DateTime?output1Time,
            DateTime?output2Time,
            bool expectedAnyOutOfDate,
            bool includeInput1,
            bool includeInput2,
            bool includeOutput1,
            bool includeOutput2
        )
        {
            List <string> inputs  = new List <string>();
            List <string> outputs = new List <string>();

            string input1  = "NONEXISTENT_FILE";
            string input2  = "NONEXISTENT_FILE";
            string output1 = "NONEXISTENT_FILE";
            string output2 = "NONEXISTENT_FILE";

            try
            {
                if (input1Time != null)
                {
                    input1 = FileUtilities.GetTemporaryFile();
                    File.WriteAllText(input1, String.Empty);
                    File.SetLastWriteTime(input1, (DateTime)input1Time);
                }

                if (input2Time != null)
                {
                    input2 = FileUtilities.GetTemporaryFile();
                    File.WriteAllText(input2, String.Empty);
                    File.SetLastWriteTime(input2, (DateTime)input2Time);
                }

                if (output1Time != null)
                {
                    output1 = FileUtilities.GetTemporaryFile();
                    File.WriteAllText(output1, String.Empty);
                    File.SetLastWriteTime(output1, (DateTime)output1Time);
                }

                if (output2Time != null)
                {
                    output2 = FileUtilities.GetTemporaryFile();
                    File.WriteAllText(output2, String.Empty);
                    File.SetLastWriteTime(output2, (DateTime)output2Time);
                }

                if (includeInput1)
                {
                    inputs.Add(input1);
                }
                if (includeInput2)
                {
                    inputs.Add(input2);
                }
                if (includeOutput1)
                {
                    outputs.Add(output1);
                }
                if (includeOutput2)
                {
                    outputs.Add(output2);
                }

                DependencyAnalysisLogDetail detail;
                Assert.Equal(expectedAnyOutOfDate, TargetUpToDateChecker.IsAnyOutOfDate(out detail, Directory.GetCurrentDirectory(), inputs, outputs));
            }
            finally
            {
                if (File.Exists(input1))
                {
                    File.Delete(input1);
                }
                if (File.Exists(input2))
                {
                    File.Delete(input2);
                }
                if (File.Exists(output1))
                {
                    File.Delete(output1);
                }
                if (File.Exists(output2))
                {
                    File.Delete(output2);
                }
            }
        }
Exemple #2
0
        private void SimpleSymlinkInputCheck(DateTime symlinkWriteTime, DateTime targetWriteTime,
                                             DateTime outputWriteTime, bool expectedOutOfDate)
        {
            var inputs = new List <string>();

            var outputs = new List <string>();

            string inputTarget  = "NONEXISTENT_FILE";
            string inputSymlink = "NONEXISTENT_FILE";
            string outputTarget = "NONEXISTENT_FILE";

            try
            {
                inputTarget = FileUtilities.GetTemporaryFile();
                _testOutputHelper.WriteLine($"Created input file {inputTarget}");
                File.SetLastWriteTime(inputTarget, targetWriteTime);

                inputSymlink = FileUtilities.GetTemporaryFile(null, ".linkin", createFile: false);

                if (!CreateSymbolicLink(inputSymlink, inputTarget, 0))
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }

                // File.SetLastWriteTime on the symlink sets the target write time,
                // so set the symlink's write time the hard way
                using (SafeFileHandle handle =
                           NativeMethodsShared.CreateFile(
                               inputSymlink, NativeMethodsShared.GENERIC_READ | 0x100 /* FILE_WRITE_ATTRIBUTES */,
                               NativeMethodsShared.FILE_SHARE_READ, IntPtr.Zero, NativeMethodsShared.OPEN_EXISTING,
                               NativeMethodsShared.FILE_ATTRIBUTE_NORMAL | NativeMethodsShared.FILE_FLAG_OPEN_REPARSE_POINT,
                               IntPtr.Zero))
                {
                    if (handle.IsInvalid)
                    {
                        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    }

                    long symlinkWriteTimeTicks = symlinkWriteTime.ToFileTimeUtc();

                    if (SetFileTime(handle, ref symlinkWriteTimeTicks, ref symlinkWriteTimeTicks,
                                    ref symlinkWriteTimeTicks) != true)
                    {
                        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    }
                }

                _testOutputHelper.WriteLine($"Created input link {inputSymlink}");

                outputTarget = FileUtilities.GetTemporaryFile();
                _testOutputHelper.WriteLine($"Created output file {outputTarget}");
                File.SetLastWriteTime(outputTarget, outputWriteTime);

                inputs.Add(inputSymlink);
                outputs.Add(outputTarget);


                DependencyAnalysisLogDetail detail;
                Assert.Equal(expectedOutOfDate,
                             TargetUpToDateChecker.IsAnyOutOfDate(out detail, Directory.GetCurrentDirectory(), inputs, outputs));
            }
            finally
            {
                if (File.Exists(inputTarget))
                {
                    File.Delete(inputTarget);
                }
                if (File.Exists(inputSymlink))
                {
                    File.Delete(inputSymlink);
                }
                if (File.Exists(outputTarget))
                {
                    File.Delete(outputTarget);
                }
            }
        }