Exemplo n.º 1
0
        public void InstallUtilTest()
        {
            Console.WriteLine("Integration testing InstallUtil by installing current test assembly '{0}' which contains installer class '{1}'", typeof(InstallUtilTests).Assembly.Location, typeof(InstallerExample).FullName);
            var target = new InstallUtil(_stubLoggingConfiguration, _folderFileSearcher, _logger);

            if (_assemblyFileInfo.Directory == null)
            {
                throw new ArgumentNullException();
            }
            Assert.AreEqual(1, _folderFileSearcher.GetFiles(_assemblyFileInfo.Directory.FullName, _includeFileSpecification).Count, "Number of assemblies to be processed by InstallUtil should be 1.");
            Assert.AreEqual(0, _folderFileSearcher.GetFiles(_assemblyFileInfo.Directory.FullName, _excludeFileSpecifications).Count, "Number of assemblies to be excluded by InstallUtil should be 0.");

            Assert.IsFalse(File.Exists(InstallerExample.InstalledFile), "'Installed' exists when it should not: " + InstallerExample.InstalledFile);
            Assert.IsFalse(File.Exists(InstallerExample.UnInstalledFile), "'UnInstalled' exists when it should not: " + InstallerExample.UnInstalledFile);

            target.Execute(InstallAction.Install, _assemblyFileInfo.Directory.FullName, _includeFileSpecification, _excludeFileSpecifications);
            Assert.IsTrue(File.Exists(InstallerExample.InstalledFile), "'Installed' file does not exist: " + InstallerExample.InstalledFile);
            Assert.IsFalse(File.Exists(InstallerExample.UnInstalledFile), "'UnInstalled' exists when it should not: " + InstallerExample.UnInstalledFile);

            target.Execute(InstallAction.UnInstall, _assemblyFileInfo.Directory.FullName, _includeFileSpecification, _excludeFileSpecifications);
            Assert.IsTrue(File.Exists(InstallerExample.UnInstalledFile), "'UnInstalled' file does not exist: " + InstallerExample.UnInstalledFile);
            Assert.IsFalse(File.Exists(InstallerExample.InstalledFile), "'Installed' exists when it should not: " + InstallerExample.InstalledFile);

            target.Execute(InstallAction.UnInstallInstall, _assemblyFileInfo.Directory.FullName, _includeFileSpecification, _excludeFileSpecifications);
            Assert.IsTrue(File.Exists(InstallerExample.InstalledFile), "'Installed' file does not exist: " + InstallerExample.InstalledFile);
            Assert.IsFalse(File.Exists(InstallerExample.UnInstalledFile), "'UnInstalled' exists when it should not: " + InstallerExample.UnInstalledFile);

            target.Execute(InstallAction.UnInstall, _assemblyFileInfo.Directory.FullName, _includeFileSpecification, _excludeFileSpecifications);
            Assert.IsTrue(File.Exists(InstallerExample.UnInstalledFile), "'UnInstalled' file does not exist: " + InstallerExample.UnInstalledFile);
            Assert.IsFalse(File.Exists(InstallerExample.InstalledFile), "'Installed' exists when it should not: " + InstallerExample.InstalledFile);
        }
Exemplo n.º 2
0
        public int Execute(InstallAction installAction, string directory, List <string> includeFileSpecs, List <string> excludeFileSpecs)
        {
            var returnValue = 0;

            try
            {
                if (!Directory.Exists(directory))
                {
                    throw new DirectoryNotFoundException("Directory not found: " + directory);
                }
                if (includeFileSpecs.Count == 0)
                {
                    throw new ArgumentOutOfRangeException("includeFileSpecs", "No files are included");
                }
                _logger.InfoFormat("Action: {0}, Directory: {1}, Include files: '{2}', Exclude files: '{3}'", installAction, directory, string.Join(";", includeFileSpecs), string.Join(";", excludeFileSpecs));
                var excludedFiles = _folderFileSearcher.GetFiles(directory, excludeFileSpecs);
                var includedFiles = _folderFileSearcher.GetFiles(directory, includeFileSpecs);
                foreach (var includedFile in includedFiles.Values)
                {
                    if (excludedFiles.ContainsKey(includedFile.Name))
                    {
                        _logger.InfoFormat("Excluding from install '{0}'", includedFile.FullName);
                        continue;
                    }

                    if (installAction == InstallAction.UnInstall || installAction == InstallAction.UnInstallInstall)
                    {
                        _logger.InfoFormat("UnInstalling '{0}'...", includedFile.FullName);
                        var arguments = GetUnInstallArguments(includedFile);
                        _logger.InfoFormat("Calling the managed installer with the arguments:{0}{1}", Environment.NewLine, string.Join(" ", arguments));
                        ManagedInstallerClass.InstallHelper(arguments);
                    }

                    if (installAction == InstallAction.Install || installAction == InstallAction.UnInstallInstall)
                    {
                        _logger.InfoFormat("Installing '{0}'...", includedFile.FullName);
                        var arguments = GetInstallArguments(includedFile);
                        _logger.InfoFormat("Calling the managed installer with the arguments:{0}{1}", Environment.NewLine, string.Join(" ", arguments));
                        ManagedInstallerClass.InstallHelper(arguments);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorFormat("Failed to execute install action '{0}' on directory '{1}'. {2}", installAction, directory, ex.ToString());
                returnValue = 1;
            }
            return(returnValue);
        }