Example #1
0
        public void Can_run_a_Powershell_script_returning_a_value()
        {
            string script = @" return $self.Id ";

            var repository = new WorkItemRepositoryMock();
            var workItem   = new WorkItemMock(repository);

            workItem.Id = 1;

            repository.SetWorkItems(new[] { workItem });
            var logger = Substitute.For <ILogEvents>();

            Assert.IsNotNull(repository.GetWorkItem(1));

            var engine = new PsScriptEngine(repository, logger, Debugger.IsAttached);

            engine.LoadAndRun("test", script, workItem);

            var expected = new Collection <PSObject> {
                new PSObject(1)
            };

            logger.Received().ResultsFromScriptRun(
                "test",
                Arg.Is <Collection <PSObject> >(x => x.Select(o => o.BaseObject).SequenceEqual(expected.Select(o => o.BaseObject))));
        }
Example #2
0
        public void Can_run_a_Powershell_script_returning_a_value()
        {
            string script = @" return $self.Id ";

            var logger     = Substitute.For <ILogEvents>();
            var settings   = TestHelpers.LoadConfigFromResourceFile("NewObjects.policies", logger);
            var repository = new WorkItemRepositoryMock();
            var context    = Substitute.For <IRequestContext>();

            context.GetProjectCollectionUri().Returns(
                new System.Uri("http://localhost:8080/tfs/DefaultCollection"));
            var runtime = RuntimeContext.MakeRuntimeContext("settingsPath", settings, context, logger, (c, i, l) => repository);

            var workItem = new WorkItemMock(repository, runtime);

            workItem.Id = 1;

            repository.SetWorkItems(new[] { workItem });

            Assert.IsNotNull(repository.GetWorkItem(1));

            var engine = new PsScriptEngine(logger, Debugger.IsAttached);

            engine.LoadAndRun("test", script, workItem, repository);

            var expected = new Collection <PSObject> {
                new PSObject(1)
            };

            logger.Received().ResultsFromScriptRun(
                "test",
                Arg.Is <Collection <PSObject> >(x => x.Select(o => o.BaseObject).SequenceEqual(expected.Select(o => o.BaseObject))));
        }
Example #3
0
        public void Can_run_a_Powershell_script_interacting_with_an_object_model()
        {
            string script = @" 

$self.Fields[""x""].Value = 33
return $self.Fields[""z""].Value ";

            var logger     = Substitute.For <ILogEvents>();
            var settings   = TestHelpers.LoadConfigFromResourceFile("NewObjects.policies", logger);
            var repository = new WorkItemRepositoryMock();

            System.Func <IRuntimeContext, IScriptLibrary> scriptLibraryBuilder = (x) => Substitute.For <IScriptLibrary>();
            var context = Substitute.For <IRequestContext>();

            context.GetProjectCollectionUri().Returns(
                new System.Uri("http://localhost:8080/tfs/DefaultCollection"));
            var runtime = RuntimeContext.MakeRuntimeContext("settingsPath", settings, context, logger, (c) => repository, scriptLibraryBuilder);

            var workItem = new WorkItemMock(repository, runtime);

            workItem.Fields["z"].Value = 42;
            workItem.Fields["x"].Value = 0;
            workItem.Id = 1;

            repository.SetWorkItems(new[] { workItem });

            Assert.IsNotNull(repository.GetWorkItem(1));
            var library = Substitute.For <IScriptLibrary>();

            var engine = new PsScriptEngine(logger, Debugger.IsAttached, library);

            // sanity check
            Assert.AreEqual(42, workItem.Fields["z"].Value);

            engine.LoadAndRun("test", script, workItem, repository);

            var expected = new Collection <PSObject> {
                new PSObject(42)
            };

            Assert.AreEqual(33, workItem.Fields["x"].Value);

            logger.Received().ResultsFromScriptRun(
                "test",
                Arg.Is <Collection <PSObject> >(x => x.Select(o => o.BaseObject).SequenceEqual(expected.Select(o => o.BaseObject))));
        }
Example #4
0
        public void Can_run_a_Powershell_script_interacting_with_an_object_model()
        {
            string script = @" 

$self.Fields[""x""].Value = 33
return $self.Fields[""z""].Value ";

            var repository = new WorkItemRepositoryMock();
            var workItem   = new WorkItemMock(repository);

            workItem.Fields["z"].Value = 42;
            workItem.Fields["x"].Value = 0;
            workItem.Id = 1;

            repository.SetWorkItems(new[] { workItem });
            var logger = Substitute.For <ILogEvents>();

            Assert.IsNotNull(repository.GetWorkItem(1));

            var engine = new PsScriptEngine(repository, logger, Debugger.IsAttached);

            // sanity check
            Assert.AreEqual(42, workItem.Fields["z"].Value);

            engine.LoadAndRun("test", script, workItem);

            var expected = new Collection <PSObject> {
                new PSObject(42)
            };

            Assert.AreEqual(33, workItem.Fields["x"].Value);

            logger.Received().ResultsFromScriptRun(
                "test",
                Arg.Is <Collection <PSObject> >(x => x.Select(o => o.BaseObject).SequenceEqual(expected.Select(o => o.BaseObject))));
        }