Esempio n. 1
0
        public VerifyingLog(
            string variantContext = "",
            bool createLog        = false,
            [CallerFilePath] string sourcePath = "",
            [CallerMemberName] string func     = "",
            Assembly asm = null)
        {
            if (asm == null)
            {
                asm = Assembly.GetCallingAssembly();
            }

            if (variantContext.Length != 0)
            {
                variantContext = $"_{variantContext}";
            }

            var conf = LogService.Instance.LogFactory.Configuration;

            oldTarget = conf.FindTargetByName(LogName);

            // If we have some logging active - we remove all rules to that specific target - and this will disable
            // logging, but will keep VerifyingLogTarget alive. Later on in Dispose we restore VerifyingLogTarget
            // name and rules.
            if (oldTarget != null)
            {
                var list = conf.AllTargets.ToList();
                for (int i = 1; ; i++)
                {
                    oldLoggerName = $"{LogName}_old_{i}"; // Rename VerifyingLogTarget =>
                                                          // VerifyingLogTarget_old_1, VerifyingLogTarget_old_2 and so on...
                    if (list.Where(x => x.Name == oldLoggerName).FirstOrDefault() == null)
                    {
                        break;
                    }
                }

                oldTarget.Name = oldLoggerName;

                var loggingRules = conf.LoggingRules;
                for (int i = 0; i < loggingRules.Count; i++)
                {
                    if (loggingRules[i].Targets.Contains(oldTarget))
                    {
                        loggingRules.RemoveAt(i);
                        i--;
                        continue;
                    }
                }
            }

            string srcpath       = CallerFilePathHelper.CallerFilePathToSolutionSourcePath(sourcePath, asm);
            string srcdir        = Path.GetDirectoryName(srcpath);
            string logdir        = Path.Combine(srcdir, Path.GetFileNameWithoutExtension(srcpath));
            string targetLogPath = Path.Combine(logdir, $"{func}{variantContext}.txt");

            ApplyTarget(new VerifyingLogTarget(LogName, targetLogPath, createLog), false);
        }
Esempio n. 2
0
        public void PackContext(
            Action <ChocolateyConfiguration> confPatch = null,
            string nuspecFile = "test1.nuspec",
            [CallerMemberName] string testFolder = "",
            [CallerFilePath] string sourcePath   = ""
            )
        {
            var conf = Scenario.pack(true);

            InstallContext.Instance.RootLocation = PrepareTestFolder(ChocoTestContext.empty, conf, Path.Combine(nameof(TestPackCommand), testFolder));

            string rootDir = InstallContext.Instance.RootLocation;

            // ChocolateyPackCommand.run can also operate via Directory.SetCurrentDirectory
            // which does not work with multitasking.
            //Directory.SetCurrentDirectory(rootDir);

            string srcpath    = CallerFilePathHelper.CallerFilePathToSolutionSourcePath(sourcePath);
            string srcdir     = Path.GetDirectoryName(srcpath);
            string nuspecPath = Path.Combine(srcdir, nuspecFile);

            File.Copy(nuspecPath, Path.Combine(rootDir, nuspecFile), true);
            File.Copy(srcpath, Path.Combine(rootDir, "justsomefile.cs"), true);

            conf.Input           = Path.Combine(rootDir, nuspecFile);
            conf.OutputDirectory = rootDir;

            var listBeforeUpdate = GetFileListing(rootDir);

            try
            {
                if (confPatch != null)
                {
                    confPatch(conf);
                }
                packcmd.run(conf);
            }
            catch (InvalidOperationException ex)
            {
                LogService.console.Info($"InvalidOperationException: {ex.Message}");
            }

            DisplayUpdates(listBeforeUpdate);
        }
Esempio n. 3
0
 public string GetFilePath(MemberInfo mi)
 {
     return(CallerFilePathHelper.CallerFilePathToSolutionSourcePath(FilePath, mi.DeclaringType.Assembly));
 }