コード例 #1
0
        public void TryGetStream_WhenFileDoesNotExist_ReturnsNullAndFalse()
        {
            var fileInfo = new FileInfo(@"C:\This\File\Does\Not\Exist.xxx");

            using (var inspector = new LazyFileInspector(fileInfo))
            {
                Stream returnedStream;
                Assert.IsFalse(inspector.TryGetStream(out returnedStream));
                Assert.IsNull(returnedStream);
            }
        }
コード例 #2
0
        public void TryGetContents_WhenFileDoesNotExist_ReturnsNullAndFalse()
        {
            var fileInfo = new FileInfo(@"C:\This\File\Does\Not\Exist.xxx");

            using (var inspector = new LazyFileInspector(fileInfo))
            {
                string returnedContents;
                Assert.IsFalse(inspector.TryGetContents(out returnedContents));
                Assert.IsNull(returnedContents);
            }
        }
コード例 #3
0
        public void TryGetFileInfo_Always_ReturnsInitializedFileInfoAndTrue()
        {
            var fileInfo = new FileInfo(@"C:\foo.txt");

            using (var inspector = new LazyFileInspector(fileInfo))
            {
                FileInfo returnedFileInfo;
                Assert.IsTrue(inspector.TryGetFileInfo(out returnedFileInfo));
                Assert.AreSame(fileInfo, returnedFileInfo);
            }
        }
コード例 #4
0
        public void TryGetContents_WhenFileExists_ReturnsContentsAndTrue()
        {
            string path = SpecialPathPolicy.For <LazyFileInspectorTest>().CreateTempFileWithUniqueName().FullName;

            File.WriteAllText(path, "Contents");

            var fileInfo = new FileInfo(path);

            using (var inspector = new LazyFileInspector(fileInfo))
            {
                string returnedContents;
                Assert.IsTrue(inspector.TryGetContents(out returnedContents));
                Assert.AreEqual("Contents", returnedContents);
            }
        }
コード例 #5
0
        public void TryGetStream_WhenFileExists_ReturnsStreamAndTrue()
        {
            string path = SpecialPathPolicy.For <LazyFileInspectorTest>().CreateTempFileWithUniqueName().FullName;

            File.WriteAllText(path, "Contents");

            var fileInfo = new FileInfo(path);

            using (var inspector = new LazyFileInspector(fileInfo))
            {
                Stream returnedStream;
                Assert.IsTrue(inspector.TryGetStream(out returnedStream));
                string contents = new StreamReader(returnedStream).ReadToEnd();
                Assert.AreEqual("Contents", contents);
            }
        }
コード例 #6
0
        public void Dispose_WhenStreamWasOpen_ShouldCloseTheStream()
        {
            string path = SpecialPathPolicy.For <LazyFileInspectorTest>().CreateTempFileWithUniqueName().FullName;

            File.WriteAllText(path, "Contents");

            var fileInfo = new FileInfo(path);

            using (var inspector = new LazyFileInspector(fileInfo))
            {
                Stream returnedStream;
                inspector.TryGetStream(out returnedStream);

                inspector.Dispose();

                Assert.DoesNotThrow(() => File.Delete(path), "Should be able to delete the file because the stream was closed.");
            }
        }
コード例 #7
0
        public void TryGetStream_WhenPreviouslyReturnedStreamWasClosed_ReturnsNewStream()
        {
            string path = SpecialPathPolicy.For <LazyFileInspectorTest>().CreateTempFileWithUniqueName().FullName;

            File.WriteAllText(path, "Contents");

            var fileInfo = new FileInfo(path);

            using (var inspector = new LazyFileInspector(fileInfo))
            {
                Stream returnedStream;
                inspector.TryGetStream(out returnedStream);
                returnedStream.Close();

                Stream secondReturnedStream;
                Assert.IsTrue(inspector.TryGetStream(out secondReturnedStream));
                Assert.AreNotSame(returnedStream, secondReturnedStream, "Should have returned a new stream since the previous one was closed.");
            }
        }
コード例 #8
0
        public void TryGetStream_WhenPreviouslyReturnedStreamStillOpen_ReturnsSameStreamAtPositionZero()
        {
            string path = SpecialPathPolicy.For <LazyFileInspectorTest>().CreateTempFileWithUniqueName().FullName;

            File.WriteAllText(path, "Contents");

            var fileInfo = new FileInfo(path);

            using (var inspector = new LazyFileInspector(fileInfo))
            {
                Stream returnedStream;
                inspector.TryGetStream(out returnedStream);
                returnedStream.Position = 2;

                Stream secondReturnedStream;
                Assert.IsTrue(inspector.TryGetStream(out secondReturnedStream));
                Assert.AreEqual(0, secondReturnedStream.Position, "Should have moved stream position back to 0.");
                Assert.AreSame(returnedStream, secondReturnedStream, "Should have returned same stream since it was still open.");
            }
        }
コード例 #9
0
        SelectTestFrameworksForFilesImpl(
            IEnumerable <ComponentHandle <ITestFramework, TestFrameworkTraits> > filteredTestFrameworkHandlesWithoutFallback,
            TestFrameworkFallbackMode testFrameworkFallbackMode,
            TestFrameworkOptions testFrameworkOptions,
            IEnumerable <FileInfo> files)
        {
            var selections = new Dictionary <object, TestFrameworkSelection>();
            var partitions = new MultiMap <TestFrameworkSelection, FileInfo>();

            foreach (var file in files)
            {
                try
                {
                    bool supported = false;
                    IList <AssemblyName> assemblyReferences = null;
                    using (var fileInspector = new LazyFileInspector(file))
                    {
                        FileType fileType = fileTypeManager.IdentifyFileType(fileInspector);

                        foreach (var testFrameworkHandle in filteredTestFrameworkHandlesWithoutFallback)
                        {
                            TestFrameworkTraits traits = testFrameworkHandle.GetTraits();
                            if (!traits.IsFrameworkCompatibleWithFileType(fileType, fileTypeManager))
                            {
                                continue;
                            }

                            // If the file is an assembly, then filter further by assembly references.
                            if (fileType.IsSameOrSubtypeOf(fileTypeManager.GetFileTypeById("Assembly")))
                            {
                                if (assemblyReferences == null)
                                {
                                    assemblyReferences = GetAssemblyReferences(fileInspector);
                                }

                                if (!traits.IsFrameworkCompatibleWithAssemblyReferences(assemblyReferences))
                                {
                                    continue;
                                }
                            }

                            TestFrameworkSelection selection = GetOrCreateSelectionIfAbsent(
                                selections, testFrameworkHandle,
                                () => new TestFrameworkSelection(testFrameworkHandle, testFrameworkOptions, false));

                            partitions.Add(selection, file);
                            supported = true;
                        }
                    }

                    if (!supported)
                    {
                        TestFrameworkSelection selection = GetFallbackSelection(selections, assemblyReferences,
                                                                                filteredTestFrameworkHandlesWithoutFallback, testFrameworkOptions, testFrameworkFallbackMode);
                        if (selection != null)
                        {
                            partitions.Add(selection, file);
                        }
                    }
                }
                catch (IOException exception)
                {
                    logger.Log(LogSeverity.Error, string.Format("Skipping file '{0}' due to IO error.", file.FullName), exception);
                }
                catch (UnauthorizedAccessException exception)
                {
                    logger.Log(LogSeverity.Error, string.Format("Skipping file '{0}' due to security error", file.FullName), exception);
                }
            }

            return(partitions);
        }