/// <summary> /// Find and reads a chutzpah test settings file given a directory. If none is found a default settings object is created /// </summary> /// <param name="directory"></param> /// <returns></returns> /// public ChutzpahTestSettingsFile FindSettingsFileFromDirectory(string directory, ChutzpahSettingsFileEnvironments environments = null) { if (string.IsNullOrEmpty(directory)) { return(ChutzpahTestSettingsFile.Default); } directory = directory.TrimEnd('/', '\\'); ChutzpahTestSettingsFile settings; if (!ChutzpahSettingsFileCache.TryGetValue(directory, out settings)) { ChutzpahSettingsFileEnvironment environment = null; if (environments != null) { environment = environments.GetSettingsFileEnvironment(directory); } return(FindSettingsFile(directory, environment).InheritFromDefault()); } else { return(settings); } }
private void UpdateChutzpahEnvironmentForProject(string projectPath) { var buildProject = ProjectCollection.GlobalProjectCollection.LoadedProjects .FirstOrDefault(x => x.FullPath.Equals(projectPath, StringComparison.OrdinalIgnoreCase)); var chutzpahEnvProps = new Collection <ChutzpahSettingsFileEnvironmentProperty>(); if (buildProject != null) { var dirPath = Path.GetDirectoryName(projectPath); foreach (var prop in ChutzpahMsBuildProps.GetProps()) { chutzpahEnvProps.Add(new ChutzpahSettingsFileEnvironmentProperty(prop, buildProject.GetPropertyValue(prop))); } lock (sync) { var envProps = settingsMapper.Settings.ChutzpahSettingsFileEnvironments .FirstOrDefault(x => x.Path.TrimEnd('/', '\\').Equals(dirPath, StringComparison.OrdinalIgnoreCase)); if (envProps == null) { envProps = new ChutzpahSettingsFileEnvironment(dirPath); settingsMapper.Settings.ChutzpahSettingsFileEnvironments.Add(envProps); } envProps.Properties = chutzpahEnvProps; } } }
private void AddSettingsFileEnvironment(string environmentStr) { if (string.IsNullOrEmpty(environmentStr)) { throw new ArgumentException( "missing argument for /settingsFileEnvironment. Expecting the settings file path and at least one property (e.g. settingsFilePath;prop1=val1;prop2=val2)"); } var envParts = environmentStr.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries); if (envParts.Length < 2) { throw new ArgumentException( "invalid argument for /settingsFileEnvironment. Expecting the settings file path and at least one property (e.g. settingsFilePath;prop1=val1;prop2=val2)"); } var path = envParts[0]; var environment = new ChutzpahSettingsFileEnvironment(path); for (var i = 1; i < envParts.Length; i++) { var propParts = envParts[i].Split('='); var name = propParts[0]; var value = propParts.Length > 1 ? propParts[1] : ""; environment.Properties.Add(new ChutzpahSettingsFileEnvironmentProperty(name, value)); } SettingsFileEnvironments.AddEnvironment(environment); }
private void InitializeSettingsFileEnvironments() { var newEnvironments = new ChutzpahSettingsFileEnvironments(); var buildProjects = ProjectCollection.GlobalProjectCollection.LoadedProjects .Where(x => !x.FullPath.Equals(".user", StringComparison.OrdinalIgnoreCase)); foreach (var buildProject in buildProjects) { var dirPath = buildProject.DirectoryPath; var environment = new ChutzpahSettingsFileEnvironment(dirPath); foreach (var prop in ChutzpahMsBuildProps.GetProps()) { var value = buildProject.GetPropertyValue(prop); if (!string.IsNullOrEmpty(value)) { environment.Properties.Add(new ChutzpahSettingsFileEnvironmentProperty(prop, value)); } } if (environment.Properties.Any()) { newEnvironments.AddEnvironment(environment); } } settingsEnvironments = newEnvironments; }
private ChutzpahTestSettingsFile FindSettingsFile(string directory, ChutzpahSettingsFileEnvironment environment) { if (string.IsNullOrEmpty(directory)) { return(ChutzpahTestSettingsFile.Default); } directory = directory.TrimEnd('/', '\\'); ChutzpahTestSettingsFile settings; if (!ChutzpahSettingsFileCache.TryGetValue(directory, out settings)) { var testSettingsFilePath = fileProbe.FindTestSettingsFile(directory); if (string.IsNullOrEmpty(testSettingsFilePath)) { ChutzpahTracer.TraceInformation("Chutzpah.json file not found given starting directoy {0}", directory); settings = ChutzpahTestSettingsFile.Default; } else if (!ChutzpahSettingsFileCache.TryGetValue(Path.GetDirectoryName(testSettingsFilePath), out settings)) { ChutzpahTracer.TraceInformation("Chutzpah.json file found at {0} given starting directoy {1}", testSettingsFilePath, directory); settings = serializer.DeserializeFromFile <ChutzpahTestSettingsFile>(testSettingsFilePath); if (settings == null) { settings = ChutzpahTestSettingsFile.Default; } else { settings.IsDefaultSettings = false; } settings.SettingsFileDirectory = Path.GetDirectoryName(testSettingsFilePath); var chutzpahVariables = BuildChutzpahReplacementVariables(testSettingsFilePath, environment, settings); ResolveTestHarnessDirectory(settings, chutzpahVariables); ResolveAMDBaseUrl(settings, chutzpahVariables); ResolveBatchCompileConfiguration(settings, chutzpahVariables); ProcessPathSettings(settings, chutzpahVariables); ProcessInheritance(environment, settings, chutzpahVariables); // Add a mapping in the cache for the directory that contains the test settings file ChutzpahSettingsFileCache.TryAdd(settings.SettingsFileDirectory, settings); } // Add mapping in the cache for the original directory tried to skip needing to traverse the tree again ChutzpahSettingsFileCache.TryAdd(directory, settings); } return(settings); }
public void Will_normalize_slashes_and_casing() { var environment = new ChutzpahSettingsFileEnvironment("path"); environment.Path = @"C:/Some/path"; environment.Properties.Add(new ChutzpahSettingsFileEnvironmentProperty("name1", "val1")); var environmentsWrapper = new ChutzpahSettingsFileEnvironments(new[] { environment }); var matched = environmentsWrapper.GetSettingsFileEnvironment(@"c:\some\path\chutzpah.json"); Assert.Equal(environment, matched); }
public void Will_match_environment_given_chutzpah_json_path() { var environment = new ChutzpahSettingsFileEnvironment("path"); environment.Path = @"C:\some\path"; environment.Properties.Add(new ChutzpahSettingsFileEnvironmentProperty("name1", "val1")); var environmentsWrapper = new ChutzpahSettingsFileEnvironments(new[] { environment }); var matched = environmentsWrapper.GetSettingsFileEnvironment(@"c:\some\path\chutzpah.json"); Assert.Equal(environment, matched); }
public void Will_expand_variables_from_passed_in_environment() { var service = new TestableChutzpahTestSettingsService(); var environment = new ChutzpahSettingsFileEnvironment("path"); environment.Path = @"dir7"; environment.Properties.Add(new ChutzpahSettingsFileEnvironmentProperty("SomeName", "SomeValue")); var varStr = "%SomeName%"; var settings = new ChutzpahTestSettingsFile { Compile = new BatchCompileConfiguration { Executable = string.Format("path {0} ok", varStr), Arguments = string.Format("path {0} ok", varStr), Paths = new List <CompilePathMap> { new CompilePathMap { OutputPath = string.Format("path {0} ok", varStr) } }, }, References = new [] { new SettingsFileReference { Path = varStr, Include = varStr, Exclude = varStr } }, Tests = new [] { new SettingsFileTestPath { Path = varStr, Include = varStr, Exclude = varStr } }, Transforms = new [] { new TransformConfig { Path = varStr } }, AMDBasePath = varStr, AMDBaseUrl = varStr }; service.Mock <IFileProbe>().Setup(x => x.FindTestSettingsFile(It.IsAny <string>())).Returns(@"C:\settingsDir7\settingsFile.json"); service.Mock <IJsonSerializer>().Setup(x => x.DeserializeFromFile <ChutzpahTestSettingsFile>(It.IsAny <string>())).Returns(settings); service.ClassUnderTest.FindSettingsFileFromDirectory("dir7", new ChutzpahSettingsFileEnvironments(new [] { environment })); Assert.True(settings.Compile.Executable.Contains("SomeValue")); Assert.True(settings.Compile.Arguments.Contains("SomeValue")); Assert.True(settings.Compile.Paths.First().OutputPath.Contains("somevalue")); Assert.True(settings.References.ElementAt(0).Path.Contains("SomeValue")); Assert.True(settings.References.ElementAt(0).Includes[0].Contains("SomeValue")); Assert.True(settings.References.ElementAt(0).Excludes[0].Contains("SomeValue")); Assert.True(settings.Tests.ElementAt(0).Path.Contains("SomeValue")); Assert.True(settings.Tests.ElementAt(0).Includes[0].Contains("SomeValue")); Assert.True(settings.Tests.ElementAt(0).Excludes[0].Contains("SomeValue")); Assert.True(settings.Transforms.ElementAt(0).Path.Contains("SomeValue")); Assert.True(settings.AMDBasePath.Contains("SomeValue")); Assert.True(settings.AMDBaseUrl.Contains("SomeValue")); }
public void Will_choose_longest_matching_path() { var environment1 = new ChutzpahSettingsFileEnvironment("path"); environment1.Path = @"C:\some\"; environment1.Properties.Add(new ChutzpahSettingsFileEnvironmentProperty("name1", "val1")); var environment2 = new ChutzpahSettingsFileEnvironment("path"); environment2.Path = @"C:\some\path\"; environment2.Properties.Add(new ChutzpahSettingsFileEnvironmentProperty("name2", "val2")); var environmentsWrapper = new ChutzpahSettingsFileEnvironments(new[] { environment1, environment2 }); var matched = environmentsWrapper.GetSettingsFileEnvironment(@"c:\some\path\chutzpah.json"); Assert.Equal(environment2, matched); }
private void ProcessInheritance(ChutzpahSettingsFileEnvironment environment, ChutzpahTestSettingsFile settings, IDictionary <string, string> chutzpahVariables) { if (settings.InheritFromParent || !string.IsNullOrEmpty(settings.InheritFromPath)) { if (string.IsNullOrEmpty(settings.InheritFromPath)) { ChutzpahTracer.TraceInformation("Searching for parent Chutzpah.json to inherit from"); settings.InheritFromPath = Path.GetDirectoryName(settings.SettingsFileDirectory); } else { ChutzpahTracer.TraceInformation("Searching for Chutzpah.json to inherit from at {0}", settings.InheritFromPath); string settingsToInherit = ExpandVariable(chutzpahVariables, settings.InheritFromPath); if (settingsToInherit.EndsWith(Constants.SettingsFileName, StringComparison.OrdinalIgnoreCase)) { settingsToInherit = Path.GetDirectoryName(settingsToInherit); } settingsToInherit = ResolveFolderPath(settings, settingsToInherit); settings.InheritFromPath = settingsToInherit; } // If we have any environment properties do not use cached // parents and re-evaluate using current environment var forceFresh = environment != null && environment.Properties.Any(); var parentSettingsFile = ProcessSettingsFile(settings.InheritFromPath, environment, forceFresh); if (!parentSettingsFile.IsDefaultSettings) { ChutzpahTracer.TraceInformation("Found parent Chutzpah.json in directory {0}", parentSettingsFile.SettingsFileDirectory); settings.InheritFrom(parentSettingsFile); } else { ChutzpahTracer.TraceInformation("Could not find a parent Chutzpah.json"); } } }
private void ProcessInheritance(ChutzpahSettingsFileEnvironment environment, ChutzpahTestSettingsFile settings, IDictionary <string, string> chutzpahVariables) { if (settings.InheritFromParent || !string.IsNullOrEmpty(settings.InheritFromPath)) { if (string.IsNullOrEmpty(settings.InheritFromPath)) { ChutzpahTracer.TraceInformation("Searching for parent Chutzpah.json to inherit from"); settings.InheritFromPath = Path.GetDirectoryName(settings.SettingsFileDirectory); } else { ChutzpahTracer.TraceInformation("Searching for Chutzpah.json to inherit from at {0}", settings.InheritFromPath); string settingsToInherit = ExpandVariable(chutzpahVariables, settings.InheritFromPath); if (settingsToInherit.EndsWith(Constants.SettingsFileName, StringComparison.OrdinalIgnoreCase)) { settingsToInherit = Path.GetDirectoryName(settingsToInherit); } settingsToInherit = ResolveFolderPath(settings, settingsToInherit); settings.InheritFromPath = settingsToInherit; } var parentSettingsFile = FindSettingsFile(settings.InheritFromPath, environment); if (!parentSettingsFile.IsDefaultSettings) { ChutzpahTracer.TraceInformation("Found parent Chutzpah.json in directory {0}", parentSettingsFile.SettingsFileDirectory); settings.InheritFrom(parentSettingsFile); } else { ChutzpahTracer.TraceInformation("Could not find a parent Chutzpah.json"); } } }
private IDictionary <string, string> BuildChutzpahReplacementVariables(string settingsFilePath, ChutzpahSettingsFileEnvironment environment, ChutzpahTestSettingsFile settings) { IDictionary <string, string> chutzpahVariables = new Dictionary <string, string>(); var clrDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(); var msbuildExe = Path.Combine(clrDir, "msbuild.exe"); var powershellExe = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"windowspowershell\v1.0\powershell.exe"); AddChutzpahVariable(chutzpahVariables, "chutzpahsettingsdir", settings.SettingsFileDirectory); AddChutzpahVariable(chutzpahVariables, "clrdir", clrDir); AddChutzpahVariable(chutzpahVariables, "msbuildexe", msbuildExe); AddChutzpahVariable(chutzpahVariables, "powershellexe", powershellExe); // This is not needed but it is a nice alias AddChutzpahVariable(chutzpahVariables, "cmdexe", Environment.ExpandEnvironmentVariables("%comspec%")); if (environment != null) { // See if we have a settingsfileenvironment set and if so add its properties as chutzpah settings file variables var props = environment.Properties; foreach (var prop in props) { AddChutzpahVariable(chutzpahVariables, prop.Name, prop.Value); } } return(chutzpahVariables); }