public void BuildFileConstructorShouldInitializePropertiesCollection() { BuildFile file = new BuildFile(@"BuildFiles\DefaultConsoleApplication.csproj"); Assert.IsNotNull(file.Properties); Assert.AreEqual<int>(22, file.Properties.Count); // Test properties. IList<BuildProperty> properties; properties = file.FindProperties("DebugType"); Assert.AreEqual<int>(2, properties.Count); Assert.AreEqual<string>("full", properties[0].Value); Assert.AreEqual<string>("pdbonly", properties[1].Value); properties = file.FindProperties("DebugType", "debug"); Assert.AreEqual<int>(1, properties.Count); Assert.AreEqual<string>("full", properties[0].Value); // Test regular property values. Assert.AreEqual<string>("Debug", file.GetPropertyValue("Configuration")); Assert.AreEqual<string>("AnyCPU", file.GetPropertyValue("Platform")); Assert.AreEqual<string>("8.0.50727", file.GetPropertyValue("ProductVersion")); Assert.AreEqual<string>("8.0.50727", file.GetPropertyValue("ProductVersion", null)); Assert.AreEqual<string>("8.0.50727", file.GetPropertyValue("ProductVersion", "")); // Test invalid property values, e.g. because of case sensitivity. Assert.IsNull(file.GetPropertyValue("configuration")); Assert.IsNull(file.GetPropertyValue("Configuration ")); // Test invalid conditions. Assert.IsNull(file.GetPropertyValue("ProductVersion", "dummy")); Assert.AreEqual<bool>(true, file.GetPropertyValueAsBoolean("DebugSymbols", "dummy", true)); Assert.AreEqual<bool>(false, file.GetPropertyValueAsBoolean("DebugSymbols", "dummy", false)); // Test valid conditions. Assert.AreEqual<string>("true", file.GetPropertyValue("DebugSymbols", "debug")); Assert.AreEqual<bool>(true, file.GetPropertyValueAsBoolean("DebugSymbols", "debug", false)); Assert.AreEqual<string>("full", file.GetPropertyValue("DebugType", "debug")); Assert.AreEqual<string>("pdbonly", file.GetPropertyValue("DebugType", "release")); }
/// <summary> /// Checks the current rule on the given build file. /// </summary> /// <param name="project">The build file to verify.</param> /// <returns>The log entries for the specified build file.</returns> public override IList <LogEntry> Check(BuildFile project) { List <LogEntry> entries = new List <LogEntry>(); foreach (string condition in new string[] { "Debug", "Release" }) { IList <BuildProperty> properties = project.FindProperties("DocumentationFile", condition); if (properties.Count == 0) { LogEntry entry = CreateMissingDocumentationFileLogEntry(condition); entries.Add(entry); } else { foreach (BuildProperty property in properties) { if (string.IsNullOrEmpty(property.Value)) { LogEntry entry = CreateMissingDocumentationFileLogEntry(condition); entries.Add(entry); } else { string outputPath = project.GetPropertyValue("OutputPath", condition); string expectedValue = Path.Combine(outputPath, project.GetPropertyValue("AssemblyName")) + ".xml"; if (!string.Equals(expectedValue, property.Value, StringComparison.OrdinalIgnoreCase)) { string message = string.Format(CultureInfo.CurrentCulture, "The documentation file has an incorrect file name."); string detail = string.Format(CultureInfo.CurrentCulture, "The XML documentation file is named \"{0}\" but it should be \"{1}\"", property.Value, expectedValue); entries.Add(new LogEntry(Name, "IncorrectFileName", LogLevel.Error, message, detail)); } } } } } return(entries); }
/// <summary> /// Checks the current rule on the given build file. /// </summary> /// <param name="project">The build file to verify.</param> /// <returns>The log entries for the specified build file.</returns> public override IList <LogEntry> Check(BuildFile project) { List <LogEntry> entries = new List <LogEntry>(); foreach (ruleElementBuildProperty expectedProperty in config.buildProperties) { StringComparison comparisonType = (StringComparison)Enum.Parse(typeof(StringComparison), expectedProperty.stringCompareOption, true); IList <BuildProperty> properties = project.FindProperties(expectedProperty.name, expectedProperty.condition); if (StringMatchesEnum(expectedProperty.compareOption, CompareOption.Exists)) { if (properties.Count == 0) { string condition = GetConditionSubstring(expectedProperty); string message = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\" should exist.", expectedProperty.name); string detail = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} was not found but should exist in the build file.", expectedProperty.name, condition); entries.Add(new LogEntry(Name, "PropertyShouldExist", LogLevel.Error, message, detail)); } } else if (StringMatchesEnum(expectedProperty.compareOption, CompareOption.DoesNotExist)) { if (properties.Count != 0) { string condition = GetConditionSubstring(expectedProperty); string message = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\" should not exist.", expectedProperty.name); string detail = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} was found but should not exist in the build file.", expectedProperty.name, condition); entries.Add(new LogEntry(Name, "PropertyShouldNotExist", LogLevel.Error, message, detail)); } } else if (StringMatchesEnum(expectedProperty.compareOption, CompareOption.EqualTo)) { if (properties.Count == 0) { string condition = GetConditionSubstring(expectedProperty); string message = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\" was not found.", expectedProperty.name); string detail = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} does not exist in the build file.", expectedProperty.name, condition); entries.Add(new LogEntry(Name, "PropertyShouldExist", LogLevel.Error, message, detail)); } else if (string.IsNullOrEmpty(expectedProperty.condition) && // No condition: either global property, or should exist for all conditions !(properties.Count == 1 && string.IsNullOrEmpty(properties[0].Condition)) && // If only one entry in project and it's condition is empty means it is a global property properties.Count != project.Conditions.Count) // If not a global property, then check that it is present for all conditions { string condition = GetConditionSubstring(expectedProperty); string message = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\" was not found in all configurations.", expectedProperty.name); string detail = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} does not exist in the build file for all configurations.", expectedProperty.name, condition); entries.Add(new LogEntry(Name, "PropertyShouldExist", LogLevel.Error, message, detail)); } else { foreach (BuildProperty property in properties) { if (!string.Equals(property.Value, expectedProperty.value, comparisonType)) { string condition = GetConditionSubstring(expectedProperty); string message = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} should have the expected value \"{2}\".", expectedProperty.name, condition, expectedProperty.value); string detail = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} has the value \"{2}\" but it should be \"{3}\".", expectedProperty.name, condition, property.Value, expectedProperty.value); entries.Add(new LogEntry(Name, "IncorrectValue", LogLevel.Error, message, detail)); } } } } else if (StringMatchesEnum(expectedProperty.compareOption, CompareOption.NotEqualTo)) { foreach (BuildProperty property in properties) { if (string.Equals(property.Value, expectedProperty.value, comparisonType)) { string condition = GetConditionSubstring(expectedProperty); string message = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} should not have the value \"{2}\".", expectedProperty.name, condition, expectedProperty.value); string detail = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} has the value \"{2}\" but this value is not allowed.", expectedProperty.name, condition, property.Value); entries.Add(new LogEntry(Name, "IncorrectValue", LogLevel.Error, message, detail)); } } } else if (StringMatchesEnum(expectedProperty.compareOption, CompareOption.In)) { if (properties.Count == 0) { string condition = GetConditionSubstring(expectedProperty); string message = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\" was not found.", expectedProperty.name); string detail = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} does not exist in the build file.", expectedProperty.name, condition); entries.Add(new LogEntry(Name, "PropertyShouldExist", LogLevel.Error, message, detail)); } foreach (BuildProperty property in properties) { if (expectedProperty.value.IndexOf(property.Value, comparisonType) < 0) { string condition = GetConditionSubstring(expectedProperty); string message = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} should be in the list \"{2}\".", expectedProperty.name, condition, expectedProperty.value); string detail = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} has the value \"{2}\" but it should be in the list \"{3}\".", expectedProperty.name, condition, property.Value, expectedProperty.value); entries.Add(new LogEntry(Name, "IncorrectValue", LogLevel.Error, message, detail)); } } } else if (StringMatchesEnum(expectedProperty.compareOption, CompareOption.NotIn)) { foreach (BuildProperty property in properties) { if (expectedProperty.value.IndexOf(property.Value, comparisonType) >= 0) { string condition = GetConditionSubstring(expectedProperty); string message = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} should not be in the list \"{2}\".", expectedProperty.name, condition, expectedProperty.value); string detail = string.Format(CultureInfo.CurrentCulture, "The build property \"{0}\"{1} has the value \"{2}\" but it should not be in the list \"{3}\".", expectedProperty.name, condition, property.Value, expectedProperty.value); entries.Add(new LogEntry(Name, "IncorrectValue", LogLevel.Error, message, detail)); } } } } return(entries); }