// TODO: Updates legacy config. private TestHostContext Setup( [CallerMemberName] string name = "", BuildCleanOption?cleanOption = null, ExistingConfigKind existingConfigKind = ExistingConfigKind.None) { // Setup the host context. TestHostContext hc = new TestHostContext(this, name); // Create a random work path. var configStore = new Mock <IConfigurationStore>(); _workFolder = Path.Combine( Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), $"_work_{Path.GetRandomFileName()}"); var settings = new AgentSettings() { WorkFolder = _workFolder }; configStore.Setup(x => x.GetSettings()).Returns(settings); hc.SetSingleton <IConfigurationStore>(configStore.Object); // Setup the execution context. _ec = new Mock <IExecutionContext>(); List <string> warnings; _variables = new Variables(hc, new Dictionary <string, string>(), new List <MaskHint>(), out warnings); _variables.Set(Constants.Variables.System.CollectionId, CollectionId); _variables.Set(Constants.Variables.System.DefinitionId, DefinitionId); _variables.Set(Constants.Variables.Build.Clean, $"{cleanOption}"); _ec.Setup(x => x.Variables).Returns(_variables); // Store the expected tracking file path. _trackingFile = Path.Combine( _workFolder, Constants.Build.Path.SourceRootMappingDirectory, _ec.Object.Variables.System_CollectionId, _ec.Object.Variables.System_DefinitionId, Constants.Build.Path.TrackingConfigFile); // Setup the endpoint. _endpoint = new ServiceEndpoint() { Name = "Some endpoint name", Url = new Uri("http://contoso.visualstudio.com"), }; // Setup the source provider. _sourceProvider = new Mock <ISourceProvider>(); _sourceProvider .Setup(x => x.GetBuildDirectoryHashKey(_ec.Object, _endpoint)) .Returns(HashKey); hc.SetSingleton <ISourceProvider>(_sourceProvider.Object); // Store the existing config object. switch (existingConfigKind) { case ExistingConfigKind.Matching: _existingConfig = new TrackingConfig(_ec.Object, _endpoint, 1, HashKey); Assert.Equal("1", _existingConfig.BuildDirectory); break; case ExistingConfigKind.Nonmatching: _existingConfig = new TrackingConfig(_ec.Object, _endpoint, 2, NonmatchingHashKey); Assert.Equal("2", _existingConfig.BuildDirectory); break; case ExistingConfigKind.None: break; default: throw new NotSupportedException(); } // Store the new config object. if (existingConfigKind == ExistingConfigKind.Matching) { _newConfig = _existingConfig; } else { _newConfig = new TrackingConfig(_ec.Object, _endpoint, 3, HashKey); Assert.Equal("3", _newConfig.BuildDirectory); } // Setup the tracking manager. _trackingManager = new Mock <ITrackingManager>(); _trackingManager .Setup(x => x.LoadIfExists(_ec.Object, _trackingFile)) .Returns(_existingConfig); if (existingConfigKind == ExistingConfigKind.None || existingConfigKind == ExistingConfigKind.Nonmatching) { _trackingManager .Setup(x => x.Create(_ec.Object, _endpoint, HashKey, _trackingFile)) .Returns(_newConfig); if (existingConfigKind == ExistingConfigKind.Nonmatching) { _trackingManager .Setup(x => x.MarkForGarbageCollection(_ec.Object, _existingConfig)); } } else if (existingConfigKind == ExistingConfigKind.Matching) { _trackingManager .Setup(x => x.UpdateJobRunProperties(_ec.Object, _existingConfig, _trackingFile)); } else { throw new NotSupportedException(); } hc.SetSingleton <ITrackingManager>(_trackingManager.Object); // Setup the build directory manager. _buildDirectoryManager = new BuildDirectoryManager(); _buildDirectoryManager.Initialize(hc); return(hc); }
// TODO: Updates legacy config. private TestHostContext Setup( [CallerMemberName] string name = "", BuildCleanOption?cleanOption = null, ExistingConfigKind existingConfigKind = ExistingConfigKind.None) { // Setup the host context. TestHostContext hc = new TestHostContext(this, name); // Create a random work path. var configStore = new Mock <IConfigurationStore>(); _workFolder = hc.GetDirectory(WellKnownDirectory.Work); var settings = new AgentSettings() { WorkFolder = _workFolder }; configStore.Setup(x => x.GetSettings()).Returns(settings); hc.SetSingleton <IConfigurationStore>(configStore.Object); // Setup the execution context. _ec = new Mock <IExecutionContext>(); List <string> warnings; _variables = new Variables(hc, new Dictionary <string, VariableValue>(), out warnings); _variables.Set(Constants.Variables.System.CollectionId, CollectionId); _variables.Set(Constants.Variables.System.DefinitionId, DefinitionId); _variables.Set(Constants.Variables.Build.Clean, $"{cleanOption}"); _ec.Setup(x => x.Variables).Returns(_variables); // Store the expected tracking file path. _trackingFile = Path.Combine( _workFolder, Constants.Build.Path.SourceRootMappingDirectory, _ec.Object.Variables.System_CollectionId, _ec.Object.Variables.System_DefinitionId, Constants.Build.Path.TrackingConfigFile); // Setup the endpoint. _repository = new Pipelines.RepositoryResource() { Alias = "self", Type = Pipelines.RepositoryTypes.Git, Url = new Uri("http://contoso.visualstudio.com"), }; _repository.Properties.Set <String>(Pipelines.RepositoryPropertyNames.Name, "Some endpoint name"); _workspaceOptions = new Pipelines.WorkspaceOptions(); // // Setup the source provider. // _sourceProvider = new Mock<ISourceProvider>(); // _sourceProvider // .Setup(x => x.GetBuildDirectoryHashKey(_ec.Object, _repository)) // .Returns(HashKey); // hc.SetSingleton<ISourceProvider>(_sourceProvider.Object); // Store the existing config object. switch (existingConfigKind) { case ExistingConfigKind.Matching: _existingConfig = new TrackingConfig(_ec.Object, _repository, 1, HashKey); Assert.Equal("1", _existingConfig.BuildDirectory); break; case ExistingConfigKind.Nonmatching: _existingConfig = new TrackingConfig(_ec.Object, _repository, 2, NonmatchingHashKey); Assert.Equal("2", _existingConfig.BuildDirectory); break; case ExistingConfigKind.None: break; default: throw new NotSupportedException(); } // Store the new config object. if (existingConfigKind == ExistingConfigKind.Matching) { _newConfig = _existingConfig; } else { _newConfig = new TrackingConfig(_ec.Object, _repository, 3, HashKey); Assert.Equal("3", _newConfig.BuildDirectory); } // Setup the tracking manager. _trackingManager = new Mock <ITrackingManager>(); _trackingManager .Setup(x => x.LoadIfExists(_ec.Object, _trackingFile)) .Returns(_existingConfig); if (existingConfigKind == ExistingConfigKind.None || existingConfigKind == ExistingConfigKind.Nonmatching) { _trackingManager .Setup(x => x.Create(_ec.Object, _repository, HashKey, _trackingFile, false)) .Returns(_newConfig); if (existingConfigKind == ExistingConfigKind.Nonmatching) { _trackingManager .Setup(x => x.MarkForGarbageCollection(_ec.Object, _existingConfig)); } } else if (existingConfigKind == ExistingConfigKind.Matching) { _trackingManager .Setup(x => x.UpdateJobRunProperties(_ec.Object, _existingConfig, _trackingFile)); } else { throw new NotSupportedException(); } hc.SetSingleton <ITrackingManager>(_trackingManager.Object); // Setup the build directory manager. _buildDirectoryManager = new BuildDirectoryManager(); _buildDirectoryManager.Initialize(hc); return(hc); }