public void GetVsixPackageFolder_should_return_executing_assembly_directory_path()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            var result = envRepos.GetVsixPackageFolder();

            // Assert
            Assert.That(result, Is.StringMatching(GetShadowCopyTargetDirectoryPattern()));
        }
        public void OpenRegistrySubKey_should_open_specified_registry_sub_key()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            using (var result = envRepos.OpenRegistrySubKey(Registry.ClassesRoot, @"TypeLib\{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}\1.1"))
            {
                // Assert
                Assert.IsNotNull(result);
            }
        }
        public void OpenRegistryBaseKey_should_open_specified_registry_key()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            using (var result = envRepos.OpenRegistryBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32))
            {
                // Assert
                Assert.AreEqual(Registry.ClassesRoot.ToString(), result.ToString());
            }
        }
        public void GetPrigPath_should_return_prig_path_in_tools_directory()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            var result = envRepos.GetPrigPath();

            // Assert
            Assert.AreEqual(@"C:\ProgramData\chocolatey\lib\Prig\tools\prig.exe", result);
        }
        public void GetRegsvr32Path_should_return_regsvr32_path_in_system32_directory()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            var result = envRepos.GetRegsvr32Path();

            // Assert
            Assert.That(result, Is.SamePath(@"C:\Windows\SysNative\regsvr32.exe"));
        }
        public void UnregisterPackageFolder_should_unregister_tools_and_lib_if_tools_does_not_exist_and_lib_exists()
        {
            using (var tools = new DirectoryInfo(Utility.Tools).BeginModifying())
            using (var lib = new DirectoryInfo(Utility.Lib).BeginModifying())
            {
                // Arrange
                var fixture = new Fixture().Customize(new AutoMoqCustomization());
                
                new DirectoryInfo(Utility.Lib).CreateWithContent(fixture);

                var envRepos = new EnvironmentRepository();

                // Act
                envRepos.UnregisterPackageFolder();

                // Assert
                Assert.IsFalse(tools.Info.Exists);
                Assert.IsFalse(lib.Info.Exists);
            }
        }
        public void GetFileDescription_should_return_the_file_description_of_the_specified_file()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            var result = envRepos.GetFileDescription(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"tools\x64\Urasandesu.Prig.dll"));

            // Assert
            Assert.That(result, Is.StringMatching(@"Prig Profiler \d+\.\d+\.\d+ Type Library"));
        }
        public void ExistsDirectory_should_return_true_if_directory_exists()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            var result = envRepos.ExistsDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));

            // Assert
            Assert.IsTrue(result);
        }
        public void UnregisterToolsPath_should_do_nothing_even_if_path_environment_variable_was_null()
        {
            // Arrange
            var fixture = new Fixture().Customize(new AutoMoqCustomization());

            var envRepos = new EnvironmentRepository();
            var mocks = new MockRepository(MockBehavior.Strict);
            {
                var m = mocks.Create<Func<string, EnvironmentVariableTarget, string>>();
                m.Setup(_ => _("ALLUSERSPROFILE", EnvironmentVariableTarget.Process)).Returns(@"C:\ProgramData");
                m.Setup(_ => _("Path", EnvironmentVariableTarget.Process)).Returns(default(string));
                m.Setup(_ => _("Path", EnvironmentVariableTarget.Machine)).Returns(default(string));
                envRepos.EnvironmentGetEnvironmentVariable = m.Object;
            }
            {
                var m = mocks.Create<Action<string, string, EnvironmentVariableTarget>>();
                envRepos.EnvironmentSetEnvironmentVariable = m.Object;
            }
            {
                var m = mocks.Create<Func<RegistryKey, string, bool, RegistryKey>>();
                envRepos.RegistryKeyOpenSubKeyStringBoolean = m.Object;
            }
            {
                var m = mocks.Create<Action<RegistryKey, string, object>>();
                envRepos.RegistryKeySetValue = m.Object;
            }


            // Act
            envRepos.UnregisterToolsPath();


            // Assert
            mocks.VerifyAll();
        }
        public void UnregisterToolsPath_should_do_nothing_if_tools_path_has_not_been_registered_yet()
        {
            // Arrange
            var fixture = new Fixture().Customize(new AutoMoqCustomization());

            var orgPath = Environment.GetEnvironmentVariable("Path");

            var envRepos = new EnvironmentRepository();


            // Act
            envRepos.UnregisterToolsPath();


            // Assert
            var path = Environment.GetEnvironmentVariable("Path");
            Assert.AreEqual(orgPath, path);
        }
        public void UnregisterToolsPath_should_remove_tools_path_from_environment_variable()
        {
            // Arrange
            var fixture = new Fixture().Customize(new AutoMoqCustomization());

            var envRepos = new EnvironmentRepository();
            envRepos.RegisterToolsPath();


            // Act
            envRepos.UnregisterToolsPath();


            // Assert
            var path = Environment.GetEnvironmentVariable("Path");
            Assert.That(path, Is.Not.StringContaining(@";C:\ProgramData\chocolatey\lib\Prig\tools"));
        }
        public void RegisterToolsPath_should_set_tools_path_even_if_path_environment_variable_was_null()
        {
            // Arrange
            var fixture = new Fixture().Customize(new AutoMoqCustomization());

            var envRepos = new EnvironmentRepository();
            var mocks = new MockRepository(MockBehavior.Strict);
            {
                var m = mocks.Create<Func<string, EnvironmentVariableTarget, string>>();
                m.Setup(_ => _("ALLUSERSPROFILE", EnvironmentVariableTarget.Process)).Returns(@"C:\ProgramData");
                m.Setup(_ => _("Path", EnvironmentVariableTarget.Process)).Returns(default(string));
                m.Setup(_ => _("Path", EnvironmentVariableTarget.Machine)).Returns(default(string));
                envRepos.EnvironmentGetEnvironmentVariable = m.Object;
            }
            {
                var m = mocks.Create<Action<string, string, EnvironmentVariableTarget>>();
                m.Setup(_ => _("Path", @"C:\ProgramData\chocolatey\lib\Prig\tools", EnvironmentVariableTarget.Process));
                envRepos.EnvironmentSetEnvironmentVariable = m.Object;
            }
            {
                var m = mocks.Create<Func<RegistryKey, string, bool, RegistryKey>>();
                m.Setup(_ => _(Registry.LocalMachine, @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment", true)).Returns(RegistryKeyMixin.DummyEnvironmentKey);
                envRepos.RegistryKeyOpenSubKeyStringBoolean = m.Object;
            }
            {
                var m = mocks.Create<Action<RegistryKey, string, object>>();
                m.Setup(_ => _(RegistryKeyMixin.DummyEnvironmentKey, "Path", @"C:\ProgramData\chocolatey\lib\Prig\tools"));
                envRepos.RegistryKeySetValue = m.Object;
            }


            // Act
            envRepos.RegisterToolsPath();


            // Assert
            mocks.VerifyAll();
        }
        public void RemovePackageFolder_should_remove_package_folder_from_environment_variable()
        {
            // Arrange
            var fixture = new Fixture().Customize(new AutoMoqCustomization());

            var variableValue = fixture.Create<string>();

            var envRepos = new EnvironmentRepository();
            envRepos.StorePackageFolder(fixture.Create<string>());


            // Act
            envRepos.RemovePackageFolder();


            // Assert
            Assert.IsNull(Environment.GetEnvironmentVariable("URASANDESU_PRIG_PACKAGE_FOLDER"));
        }
        public void StorePackageFolder_should_set_package_folder_as_environment_variable()
        {
            // Arrange
            var fixture = new Fixture().Customize(new AutoMoqCustomization());

            var variableValue = fixture.Create<string>();

            var envRepos = new EnvironmentRepository();


            // Act
            envRepos.StorePackageFolder(variableValue);


            // Assert
            Assert.AreEqual(variableValue, Environment.GetEnvironmentVariable("URASANDESU_PRIG_PACKAGE_FOLDER"));
        }
        public void UnregisterPackageFolder_should_unregister_tools_and_lib_if_tools_does_not_exist_and_lib_does_not_exist()
        {
            using (var tools = new DirectoryInfo(Utility.Tools).BeginModifying())
            using (var lib = new DirectoryInfo(Utility.Lib).BeginModifying())
            {
                // Arrange
                var envRepos = new EnvironmentRepository();

                // Act
                envRepos.UnregisterPackageFolder();

                // Assert
                Assert.IsFalse(tools.Info.Exists);
                Assert.IsFalse(lib.Info.Exists);
            }
        }
        public void GetRegistryValue_should_get_specified_registry_value()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            using (var key = Registry.ClassesRoot.OpenSubKey(@"TypeLib\{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}\1.1"))
            {
                var result = envRepos.GetRegistryValue(key, null);

                // Assert
                Assert.AreEqual("Microsoft Internet Controls", result);
            }
        }
        public void ExistsFile_should_return_true_if_file_exists()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            var result = envRepos.ExistsFile(Assembly.GetExecutingAssembly().Location);

            // Assert
            Assert.IsTrue(result);
        }
        public void GetLibPath_should_return_lib_directory_in_chocolatey_compatible_directory_path()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            var result = envRepos.GetLibPath();

            // Assert
            Assert.AreEqual(@"C:\ProgramData\chocolatey\lib\Prig\lib", result);
        }
        public void ExistsDirectory_should_return_false_if_directory_does_not_exist()
        {
            // Arrange
            var fixture = new Fixture().Customize(new AutoMoqCustomization());

            var envRepos = new EnvironmentRepository();

            // Act
            var result = envRepos.ExistsDirectory(fixture.Create<string>());

            // Assert
            Assert.IsFalse(result);
        }
        public void GetProfilerLocations_should_always_return_fixed_values()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            var results = envRepos.GetProfilerLocations();

            // Assert
            Assert.AreEqual(2, results.Length);
            Assert.AreEqual(RegistryView.Registry64, results[0].RegistryView);
            Assert.AreEqual(@"C:\ProgramData\chocolatey\lib\Prig\tools\x64\Urasandesu.Prig.dll", results[0].PathOfInstalling);
            Assert.AreEqual(RegistryView.Registry32, results[1].RegistryView);
            Assert.AreEqual(@"C:\ProgramData\chocolatey\lib\Prig\tools\x86\Urasandesu.Prig.dll", results[1].PathOfInstalling);
        }
        public void GetVsixLibPath_should_return_lib_directory_in_executing_assembly_directory_path()
        {
            // Arrange
            var envRepos = new EnvironmentRepository();

            // Act
            var result = envRepos.GetVsixLibPath();

            // Assert
            Assert.That(result, Is.StringMatching(ToShadowCopyTargetPattern(@"lib")));
        }
        public void RegisterPackageFolder_should_register_tools_and_lib_if_tools_does_not_exist_and_lib_exists()
        {
            using (var tools = new DirectoryInfo(Utility.Tools).BeginModifying())
            using (var lib = new DirectoryInfo(Utility.Lib).BeginModifying())
            {
                // Arrange
                var fixture = new Fixture().Customize(new AutoMoqCustomization());

                var dummyLibLastWriteTime = new DirectoryInfo(Utility.Lib).CreateWithContent(fixture).LastWriteTime;

                var envRepos = new EnvironmentRepository();

                // Act
                envRepos.RegisterPackageFolder();

                // Assert
                Assert.IsTrue(tools.Info.Exists);
                Assert.AreNotEqual(dummyLibLastWriteTime, lib.Info.LastWriteTime);
            }
        }