public void Resolve_NonexistentFile_ThrowsExecutableNotFoundException()
        {
            var environment = new Mock <IEnvironment>();
            var resolver    = new ExecutablePathResolver(fileSystem, environment.Object);

            Assert.Throws <ExecutableNotFoundException>(() => resolver.Resolve("C:\\non-existent-file.exe"));
        }
        public void Resolve_WithRelativePath_ReturnsAbsolutePath()
        {
            var environment = new Mock <IEnvironment>();
            var resolver    = new ExecutablePathResolver(fileSystem, environment.Object);

            var location = resolver.Resolve("..\\file.exe");

            Assert.Equal("C:\\file.exe", location);
        }
        public void Resolve_WithDirectorySeparatorChar_UsesGivenLocation()
        {
            var environment = new Mock <IEnvironment>();
            var resolver    = new ExecutablePathResolver(fileSystem, environment.Object);

            var location = resolver.Resolve("C:\\file.exe");

            Assert.Equal("C:\\file.exe", location);
        }
        public void Resolve_WithEmptyPath_ThrowsExecutableNotFoundException()
        {
            var environment = new Mock <IEnvironment>();

            environment.Setup(e => e.GetEnvironmentVariable("PATH")).Returns("");
            var resolver = new ExecutablePathResolver(fileSystem, environment.Object);

            Assert.Throws <ExecutableNotFoundException>(() => resolver.Resolve("file.exe"));
        }
        public void Resolve_NotInPath_ThrowsExecutableNotFoundException()
        {
            var environment = new Mock <IEnvironment>();

            environment.Setup(e => e.GetEnvironmentVariable("PATH")).Returns("C:\\bin");
            var resolver = new ExecutablePathResolver(fileSystem, environment.Object);

            Assert.Throws <ExecutableNotFoundException>(() => resolver.Resolve("non-existent-file"));
            environment.Verify(e => e.GetEnvironmentVariable("PATH"), Times.Once);
        }
        public void Resolve_WithNullPath_ThrowsExecutableNotFoundException()
        {
            var environment = new Mock <IEnvironment>();

            environment.Setup(e => e.GetEnvironmentVariable("PATH")).Returns((string)null);
            var resolver = new ExecutablePathResolver(fileSystem, environment.Object);

            Assert.Throws <ExecutableNotFoundException>(() => resolver.Resolve("file.exe"));
            environment.Verify(e => e.GetEnvironmentVariable("PATH"), Times.Once);
        }
        public void Resolve_WithoutDirectorySeparatorsAndExtension_GetsLocationFromPath()
        {
            var environment = new Mock <IEnvironment>();

            environment.Setup(e => e.GetEnvironmentVariable("PATH")).Returns("C:\\bin");
            var resolver = new ExecutablePathResolver(fileSystem, environment.Object);

            var location = resolver.Resolve("file");

            environment.Verify(e => e.GetEnvironmentVariable("PATH"), Times.Once);
            Assert.Equal("C:\\bin\\file.exe", location);
        }
        public void Resolve_WithInvalidPathEntries_IgnoresThem()
        {
            var environment = new Mock <IEnvironment>();

            environment.Setup(e => e.GetEnvironmentVariable("PATH"))
            .Returns(@";?;;;/bin;  ;%;'';\\invalidNetworkShare;");
            var resolver = new ExecutablePathResolver(fileSystem, environment.Object);

            var location = resolver.Resolve($"file.exe");

            Assert.Equal("C:\\bin\\file.exe", location);
        }
        public void Resolve_GetFullPathFails_IgnoresEntry()
        {
            var environment = new Mock <IEnvironment>();

            environment.Setup(e => e.GetEnvironmentVariable("PATH"))
            .Returns(@"fail;/bin");
            var fileSystemMock = new Mock <IFileSystem>();

            fileSystemMock.Setup(f => f.Path.GetFullPath("/bin")).Returns("C:\\bin");
            fileSystemMock.Setup(f => f.Path.GetFullPath("fail")).Throws <Exception>();
            fileSystemMock.Setup(f => f.Path.Combine("C:\\bin", "file.exe")).Returns("C:\\bin\\file.exe");
            fileSystemMock.Setup(f => f.File.Exists(It.IsAny <string>())).Returns(true);
            var resolver = new ExecutablePathResolver(fileSystemMock.Object, environment.Object);

            var location = resolver.Resolve($"file.exe");

            Assert.Equal("C:\\bin\\file.exe", location);
        }