public void Should_Merge_If_Property_Element_Merge()
        {
            var mockAppOptions = new Mock <IAppOptions>();

            mockAppOptions.SetupAllProperties();
            var appOptions = mockAppOptions.Object;

            appOptions.Exclude = new string[] { "global" };
            var stringArrayElement = XElement.Parse($@"
<Root defaultMerge='false'>
  <Exclude merge='true'>
    1
    2
  </Exclude>
</Root>
");

            var settingsMerger = new SettingsMerger(null);
            var mergedSettings = settingsMerger.Merge(
                appOptions,
                new List <XElement> {
            },
                stringArrayElement);

            Assert.AreSame(appOptions, mergedSettings);
            Assert.AreEqual(new string[] { "global", "1", "2" }, appOptions.Exclude);
        }
        public void Should_Not_Throw_If_Merge_Current_Null_String_Array_Type()
        {
            var mockAppOptions = new Mock <IAppOptions>();

            mockAppOptions.SetupAllProperties();
            var appOptions = mockAppOptions.Object;

            appOptions.Exclude = null;
            var stringArrayElement = XElement.Parse($@"
<Root>
  <Exclude merge='true'>
    1
    2
  </Exclude>
</Root>
");

            var settingsMerger = new SettingsMerger(null);
            var mergedSettings = settingsMerger.Merge(
                appOptions,
                new List <XElement> {
            },
                stringArrayElement);

            Assert.AreSame(appOptions, mergedSettings);
            Assert.AreEqual(new string[] { "1", "2" }, appOptions.Exclude);
        }
        /// <summary>
        /// Loads the settings for the <paramref name="projects"/> specified.
        /// </summary>
        /// <param name="projects">The projects whose settings to load.</param>
        private void LoadProjectSettings(IList <CodeProject> projects)
        {
            Settings policySettings = null;

            if (!string.IsNullOrEmpty(this.PolicySettingsPath))
            {
                Settings localSettings = this.Core.Environment.GetSettings(this.PolicySettingsPath, false);
                if (localSettings != null)
                {
                    SettingsMerger merger = new SettingsMerger(localSettings, this.Core.Environment);
                    policySettings = merger.MergedSettings;
                }
            }

            foreach (CodeProject project in projects)
            {
                Settings projectSettings = policySettings;
                if (this.Settings.AllowProjectToOverridePolicy)
                {
                    projectSettings = this.Core.Environment.GetProjectSettings(project, true);
                }

                if (projectSettings != null)
                {
                    project.Settings       = projectSettings;
                    project.SettingsLoaded = true;
                }
            }
        }
        public void Should_Overwrite_String_Array_By_Default()
        {
            var mockAppOptions = new Mock <IAppOptions>();

            mockAppOptions.SetupAllProperties();
            var appOptions = mockAppOptions.Object;

            appOptions.Exclude = new string[] { "global" };
            var stringArrayElement = XElement.Parse($@"
<Root>
  <Exclude>
    1
    2
  </Exclude>
</Root>
");

            var settingsMerger = new SettingsMerger(null);
            var mergedSettings = settingsMerger.Merge(
                appOptions,
                new List <XElement> {
            },
                stringArrayElement);

            Assert.AreSame(appOptions, mergedSettings);
            Assert.AreEqual(new string[] { "1", "2" }, appOptions.Exclude);
        }
        public void Should_Throw_For_Unsupported_Conversion()
        {
            var settingsMerger  = new SettingsMerger(new Mock <ILogger>().Object);
            var settingsElement = XElement.Parse($"<Root><PropertyType/></Root>");
            var unsupported     = typeof(PropertyInfo).GetProperty(nameof(PropertyInfo.PropertyType));
            var expectedMessage = $"Cannot handle 'PropertyType' yet";

            Assert.Throws <Exception>(() => settingsMerger.GetValueFromXml(settingsElement, unsupported), expectedMessage);
        }
        public void Should_Use_Global_Settings_If_No_Project_Level_Or_FCC_Settings_Files()
        {
            var mockAppOptions = new Mock <IAppOptions>(MockBehavior.Strict);
            var appOptions     = mockAppOptions.Object;

            var settingsMerger = new SettingsMerger(null);
            var mergedSettings = settingsMerger.Merge(appOptions, new List <XElement>(), null);

            Assert.AreSame(appOptions, mergedSettings);
        }
        public void Should_Convert_Xml_Value_Correctly(string propertyElement, string propertyName, object expectedConversion, bool expectedException)
        {
            var settingsMerger  = new SettingsMerger(new Mock <ILogger>().Object);
            var settingsElement = XElement.Parse($"<Root>{propertyElement}</Root>");
            var property        = typeof(IAppOptions).GetPublicProperties().First(p => p.Name == propertyName);

            var value = settingsMerger.GetValueFromXml(settingsElement, property);

            Assert.AreEqual(expectedConversion, value);
        }
        public void Should_Overwrite_GlobalOptions_Bool_Properties_From_Settings_File()
        {
            var mockAppOptions = new Mock <IAppOptions>(MockBehavior.Strict);

            mockAppOptions.SetupSet(o => o.IncludeReferencedProjects = true);
            var appOptions = mockAppOptions.Object;

            var settingsMerger      = new SettingsMerger(null);
            var settingsFileElement = CreateIncludeReferencedProjectsElement(true);
            var mergedSettings      = settingsMerger.Merge(appOptions, new List <XElement> {
                settingsFileElement
            }, null);

            Assert.AreSame(appOptions, mergedSettings);
        }
        public void Should_Overwrite_GlobalOptions_Bool_Properties_From_Project(bool last)
        {
            var mockAppOptions = new Mock <IAppOptions>();

            mockAppOptions.SetupAllProperties();
            var appOptions = mockAppOptions.Object;

            var settingsMerger      = new SettingsMerger(null);
            var settingsFileElement = CreateIncludeReferencedProjectsElement(!last);
            var projectElement      = CreateIncludeReferencedProjectsElement(last);
            var mergedSettings      = settingsMerger.Merge(
                appOptions,
                new List <XElement> {
                settingsFileElement
            },
                projectElement);

            Assert.AreSame(appOptions, mergedSettings);
            Assert.AreEqual(last, appOptions.IncludeReferencedProjects);
        }
Ejemplo n.º 10
0
        private Settings GetMergedSettings(string settingsPath)
        {
            StyleCopTrace.In(settingsPath);

            string cacheKey = string.Empty;

            if (string.IsNullOrEmpty(settingsPath))
            {
                cacheKey = string.Format("{0}::EMPTY", "GetMergedSettings");
            }
            else
            {
                cacheKey = string.Format("{0}::{1}", "GetMergedSettings", settingsPath.ToLowerInvariant());
            }

            Settings mergedSettings = null;

            if (this.settingsCache.TryGetValue(cacheKey, out mergedSettings))
            {
                StyleCopTrace.Out();

                return(mergedSettings);
            }

            Settings localSettings = this.styleCopCore.Environment.GetSettings(settingsPath, false);

            if (localSettings != null)
            {
                SettingsMerger merger = new SettingsMerger(localSettings, this.styleCopCore.Environment);

                mergedSettings = merger.MergedSettings;
            }

            this.settingsCache[cacheKey] = mergedSettings;

            // TODO: This doesn't invalidate the cache if any of the other files change
            this.AddWatcherForSettingsFile(settingsPath);

            return(StyleCopTrace.Out(mergedSettings));
        }
        public void Should_Overwrite_String_Properties()
        {
            var mockAppOptions = new Mock <IAppOptions>();

            mockAppOptions.SetupAllProperties();
            var appOptions = mockAppOptions.Object;

            var stringElement = XElement.Parse($@"
<Root>
    <ToolsDirectory>ToolsDirectory</ToolsDirectory>
</Root>
");

            var settingsMerger = new SettingsMerger(null);
            var mergedSettings = settingsMerger.Merge(
                appOptions,
                new List <XElement> {
            },
                stringElement);

            Assert.AreSame(appOptions, mergedSettings);
            Assert.AreEqual("ToolsDirectory", appOptions.ToolsDirectory);
        }
        public void Should_Overwrite_Int_Properties()
        {
            var mockAppOptions = new Mock <IAppOptions>();

            mockAppOptions.SetupAllProperties();
            var appOptions = mockAppOptions.Object;

            var intElement = XElement.Parse($@"
<Root>
    <ThresholdForCyclomaticComplexity>123</ThresholdForCyclomaticComplexity>
</Root>
");

            var settingsMerger = new SettingsMerger(null);
            var mergedSettings = settingsMerger.Merge(
                appOptions,
                new List <XElement> {
            },
                intElement);

            Assert.AreSame(appOptions, mergedSettings);
            Assert.AreEqual(123, appOptions.ThresholdForCyclomaticComplexity);
        }
        public void Should_Overwrite_Enum_Properties()
        {
            var mockAppOptions = new Mock <IAppOptions>();

            mockAppOptions.SetupAllProperties();
            var appOptions = mockAppOptions.Object;

            var enumElement = XElement.Parse($@"
<Root>
    <RunMsCodeCoverage>IfInRunSettings</RunMsCodeCoverage>
</Root>
");

            var settingsMerger = new SettingsMerger(null);
            var mergedSettings = settingsMerger.Merge(
                appOptions,
                new List <XElement> {
            },
                enumElement);

            Assert.AreSame(appOptions, mergedSettings);
            Assert.AreEqual(RunMsCodeCoverage.IfInRunSettings, appOptions.RunMsCodeCoverage);
        }
Ejemplo n.º 14
0
        private Settings GetMergedSettings(string settingsPath)
        {
            StyleCopTrace.In(settingsPath);

            string cacheKey = string.Empty;

            if (string.IsNullOrEmpty(settingsPath))
            {
                cacheKey = string.Format("{0}::EMPTY", "GetMergedSettings");
            }
            else
            {
                cacheKey = string.Format("{0}::{1}", "GetMergedSettings", settingsPath.ToLowerInvariant());
            }

            Settings mergedSettings = null;

            if (SettingsCache.TryGetValue(cacheKey, out mergedSettings))
            {
                StyleCopTrace.Out();

                return(mergedSettings);
            }

            Settings localSettings = this.styleCopCore.Environment.GetSettings(settingsPath, false);

            if (localSettings != null)
            {
                SettingsMerger merger = new SettingsMerger(localSettings, this.styleCopCore.Environment);

                mergedSettings = merger.MergedSettings;
            }

            SettingsCache[cacheKey] = mergedSettings;

            return(StyleCopTrace.Out(mergedSettings));
        }
        private Settings GetMergedSettings(string settingsPath)
        {
            StyleCopTrace.In(settingsPath);

            string cacheKey = string.Empty;

            if (string.IsNullOrEmpty(settingsPath))
            {
                cacheKey = string.Format("{0}::EMPTY", "GetMergedSettings");
            }
            else
            {
                cacheKey = string.Format("{0}::{1}", "GetMergedSettings", settingsPath.ToLowerInvariant());
            }

            Settings mergedSettings = null;

            if (SettingsCache.TryGetValue(cacheKey, out mergedSettings))
            {
                StyleCopTrace.Out();

                return mergedSettings;
            }

            Settings localSettings = this.styleCopCore.Environment.GetSettings(settingsPath, false);

            if (localSettings != null)
            {
                SettingsMerger merger = new SettingsMerger(localSettings, this.styleCopCore.Environment);

                mergedSettings = merger.MergedSettings;
            }

            SettingsCache[cacheKey] = mergedSettings;

            return StyleCopTrace.Out(mergedSettings);
        }
Ejemplo n.º 16
0
        private Settings GetMergedSettings(string settingsPath)
        {
            StyleCopTrace.In(settingsPath);

            string cacheKey = string.Empty;

            if (string.IsNullOrEmpty(settingsPath))
            {
                cacheKey = string.Format("{0}::EMPTY", "GetMergedSettings");
            }
            else
            {
                cacheKey = string.Format("{0}::{1}", "GetMergedSettings", settingsPath.ToLowerInvariant());
            }

            Settings mergedSettings = null;

            if (this.settingsCache.TryGetValue(cacheKey, out mergedSettings))
            {
                StyleCopTrace.Out();

                return mergedSettings;
            }

            Settings localSettings = this.styleCopCore.Environment.GetSettings(settingsPath, false);

            if (localSettings != null)
            {
                SettingsMerger merger = new SettingsMerger(localSettings, this.styleCopCore.Environment);

                mergedSettings = merger.MergedSettings;
            }

            this.settingsCache[cacheKey] = mergedSettings;

            // TODO: This doesn't invalidate the cache if any of the other files change
            this.AddWatcherForSettingsFile(settingsPath);

            return StyleCopTrace.Out(mergedSettings);
        }
    /// <summary>
    /// Initializes a new instance of the <see cref="StyleCopSettingsHandler"/> class.
    /// </summary>
    /// <param name="settingsFilePath">The path to the StyleCop settings file.</param>
    /// <param name="coreInstance">The StyleCop core instance.</param>
    internal StyleCopSettingsHandler(string settingsFilePath, StyleCopCore coreInstance)
    {
      Param.AssertValidString(settingsFilePath, "settingsFilePath");
      Param.AssertNotNull(coreInstance, "coreInstance");

      this.core = coreInstance;

      // Load the local settings.
      Exception exception = null;
      this.localSettings = this.core.Environment.GetWritableSettings(settingsFilePath, out exception);

      if (exception != null)
      {
        Gtk.MessageDialog messageDialog = new Gtk.MessageDialog(
          null,
          Gtk.DialogFlags.Modal,
          Gtk.MessageType.Error,
          Gtk.ButtonsType.Ok,
          string.Format(CultureInfo.CurrentUICulture, StaticStringResources.ProjectSettingsFileNotLoadedOrCreated, exception.Message));

        messageDialog.Title = StaticStringResources.Title;
        messageDialog.Run();
        messageDialog.Destroy();

        throw exception;
      }
      else if (this.localSettings != null)
      {
        if (string.Compare(settingsFilePath, this.core.Environment.GetDefaultSettingsPath(), true) == 0)
        {
          // We must use reflection at the moment to set the DefaultSettings property (in case it exists).
          PropertyInfo prop = this.localSettings.GetType().GetProperty("DefaultSettings", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
          if (prop != null && prop.CanWrite)
          {
            prop.SetValue(this.localSettings, true, null);
          }
        }

        // Set the contents of the parent settings file.
        SettingsMerger merger = new SettingsMerger(this.localSettings, this.core.Environment);
        this.parentSettings = merger.ParentMergedSettings;
        this.mergedSettings = merger.MergedSettings;

        // Set up the settings comparer.
        this.settingsComparer = new SettingsComparer(this.localSettings, this.parentSettings);
      }
      else
      {
        throw new InvalidOperationException("StyleCop settings couldn't be loaded!");
      }
    }