Example #1
0
        private bool Configure(Engine engine)
        {
            try
            {
                // If we have a configuration file use it, otherwise configure with defaults
                IFile configFile = engine.FileSystem.GetRootFile(_configFilePath);
                if (configFile.Exists)
                {
                    Trace.Information("Loading configuration from {0}", configFile.Path);
                    engine.Configure(configFile, _updatePackages, _outputScripts);
                }
                else
                {
                    Trace.Information("Could not find configuration file {0}, using default configuration", _configFilePath);
                    engine.Configure(GetDefaultConfigScript(), _updatePackages);
                }
            }
            catch (Exception ex)
            {
                Trace.Critical("Error while loading configuration: {0}", ex.Message);
                return false;
            }

            return true;
        }
Example #2
0
            public void Document()
            {
                // Given
                string inputFolder = Path.Combine(TestContext.CurrentContext.TestDirectory, @".\Input");
                if (!Directory.Exists(inputFolder))
                {
                    Directory.CreateDirectory(inputFolder);
                }
                IExecutionContext context = Substitute.For<IExecutionContext>();
                context.RootFolder.Returns(TestContext.CurrentContext.TestDirectory);
                context.InputFolder.Returns(inputFolder);
                Engine engine = new Engine();
                engine.Configure();
                context.Assemblies.Returns(engine.Assemblies);
                IDocument document = Substitute.For<IDocument>();
                document.Source.Returns(@"C:\Temp\temp.txt");
                document.GetStream().Returns(new MemoryStream(Encoding.UTF8.GetBytes(@"<p>@Document.Source</p>")));
                Razor razor = new Razor();

                // When
                razor.Execute(new[] { document }, context).ToList();

                // Then
                context.Received(1).GetDocument(Arg.Any<IDocument>(), Arg.Any<string>());
                context.Received().GetDocument(document, @"<p>C:\Temp\temp.txt</p>");
            }
Example #3
0
        public void CompletedContentIsPopulatedAfterRun()
        {
            // Given
            Engine engine = new Engine();
            engine.CleanOutputPathOnExecute = false;
            int c = 0;
            engine.Pipelines.Add(
                new Execute((x, ctx) => new[]
                {
                    ctx.GetDocument(x, (c++).ToString()),
                    ctx.GetDocument(x, (c++).ToString())
                }),
                new Execute((x, ctx) => new[]
                {
                    ctx.GetDocument(x, (c++).ToString())
                }),
                new Core.Modules.Metadata.Meta("Content", (x, y) => x.Content));

            // When
            engine.Execute();

            // Then
            Assert.AreEqual(2, engine.Documents.FromPipeline("Pipeline 1").Count());
            Assert.AreEqual("2", engine.Documents.FromPipeline("Pipeline 1").First().String("Content"));
            Assert.AreEqual("3", engine.Documents.FromPipeline("Pipeline 1").Skip(1).First().String("Content"));
        }
Example #4
0
        public void ExecuteResultsInCorrectCounts()
        {
            // Given
            Engine engine = new Engine();
            CountModule a = new CountModule("A")
            {
                AdditionalOutputs = 1
            };
            CountModule b = new CountModule("B")
            {
                AdditionalOutputs = 2
            };
            CountModule c = new CountModule("C")
            {
                AdditionalOutputs = 3
            };
            engine.Pipelines.Add(a, b, c);

            // When
            engine.Execute();

            // Then
            Assert.AreEqual(1, a.ExecuteCount);
            Assert.AreEqual(1, b.ExecuteCount);
            Assert.AreEqual(1, c.ExecuteCount);
            Assert.AreEqual(1, a.InputCount);
            Assert.AreEqual(2, b.InputCount);
            Assert.AreEqual(6, c.InputCount);
            Assert.AreEqual(2, a.OutputCount);
            Assert.AreEqual(6, b.OutputCount);
            Assert.AreEqual(24, c.OutputCount);
        }
Example #5
0
        private bool Execute(Engine engine)
        {
            if (!_noClean)
            {
                try
                {
                    engine.Trace.Information("Cleaning output directory {0}...", engine.OutputFolder);
                    if (Directory.Exists(engine.OutputFolder))
                    {
                        Directory.Delete(engine.OutputFolder, true);
                    }
                    engine.Trace.Information("Cleaned output directory.");
                }
                catch (Exception ex)
                {
                    engine.Trace.Critical("Error while cleaning output directory: {0}.", ex.Message);
                    return false;
                }
            }

            try
            {
                engine.Execute();
            }
            catch (Exception ex)
            {
                engine.Trace.Critical("Error while executing: {0}.", ex.Message);
                return false;
            }

            return true;
        }
Example #6
0
 public void ExecuteAllExamples()
 {
     string path = Path.Combine(Assembly.GetExecutingAssembly().Location, "Examples");
     while (!Directory.Exists(path))
     {
         path = Directory.GetParent(path).Parent.FullName;
         path = Path.Combine(path, "Examples");
     }
     int count = 0;
     foreach(string example in Directory.EnumerateDirectories(path))
     {
         Console.WriteLine("Executing example " + example);
         Engine engine = new Engine
         {
             RootFolder = example
         };
         string config = Path.Combine(example, "config.wyam");
         if (File.Exists(config))
         {
             Console.WriteLine("Loading configuration file");
             engine.Configure(File.ReadAllText(config));
         }
         engine.Execute();
         count++;
     }
     Assert.AreEqual(4, count);
 }
        public void ToLookupOfStringReturnsCorrectLookup()
        {
            // Given
            Engine engine = new Engine();
            Pipeline pipeline = new Pipeline("Pipeline", engine, null);
            IDocument a = new Document(engine, pipeline)
                .Clone("a", new[] { new KeyValuePair<string, object>("Numbers", new[] { 1, 2, 3 }) });
            IDocument b = new Document(engine, pipeline)
                .Clone("b", new[] { new KeyValuePair<string, object>("Numbers", new[] { 2, 3, 4 }) });
            IDocument c = new Document(engine, pipeline)
                .Clone("c", new[] { new KeyValuePair<string, object>("Numbers", 3) });
            IDocument d = new Document(engine, pipeline)
                .Clone("d", new[] { new KeyValuePair<string, object>("Numbers", "4") });
            List<IDocument> documents = new List<IDocument>() { a, b, c, d };

            // When
            ILookup<string, IDocument> lookup = documents.ToLookup<string>("Numbers");

            // Then
            Assert.AreEqual(4, lookup.Count);
            CollectionAssert.AreEquivalent(new[] { a }, lookup["1"]);
            CollectionAssert.AreEquivalent(new[] { a, b }, lookup["2"]);
            CollectionAssert.AreEquivalent(new[] { a, b, c }, lookup["3"]);
            CollectionAssert.AreEquivalent(new[] { b, d }, lookup["4"]);
        }
Example #8
0
        public void SimpleTemplate()
        {
            // Given
            string inputFolder = Path.Combine(Environment.CurrentDirectory, @".\Input");
            if (!Directory.Exists(inputFolder))
            {
                Directory.CreateDirectory(inputFolder);
            }
            IExecutionContext context = Substitute.For<IExecutionContext>();
            context.RootFolder.Returns(Environment.CurrentDirectory);
            context.InputFolder.Returns(inputFolder);
            Engine engine = new Engine();
            engine.Configure();
            context.Assemblies.Returns(engine.Assemblies);
            IDocument document = Substitute.For<IDocument>();
            document.GetStream().Returns(new MemoryStream(Encoding.UTF8.GetBytes(@"@for(int c = 0 ; c < 5 ; c++) { <p>@c</p> }")));
            Razor razor = new Razor();

            // When
            razor.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

            // Then
            document.Received(1).Clone(Arg.Any<string>());
            document.Received().Clone(" <p>0</p>  <p>1</p>  <p>2</p>  <p>3</p>  <p>4</p> ");
        }
Example #9
0
        public void CompletedContentIsPopulatedAfterRun()
        {
            // Given
            Engine engine = new Engine();
            int c = 0;
            engine.Pipelines.Add(
                new Execute((x, ctx) => new[]
                {
                    x.Clone((c++).ToString()),
                    x.Clone((c++).ToString())
                }),
                new Execute((x, ctx) => new[]
                {
                    x.Clone((c++).ToString())
                }),
                new Meta("Content", (x, y) => x.Content));

            // When
            engine.Execute();

            // Then
            Assert.AreEqual(2, engine.Documents.FromPipeline("Pipeline 1").Count());
            Assert.AreEqual("2", engine.Documents.FromPipeline("Pipeline 1").First().String("Content"));
            Assert.AreEqual("3", engine.Documents.FromPipeline("Pipeline 1").Skip(1).First().String("Content"));
        }
Example #10
0
        public void Tracing()
        {
            // Given
            string inputFolder = Path.Combine(Environment.CurrentDirectory, @".\Input");
            if (!Directory.Exists(inputFolder))
            {
                Directory.CreateDirectory(inputFolder);
            }
            ITrace trace = Substitute.For<ITrace>();
            IExecutionContext context = Substitute.For<IExecutionContext>();
            context.RootFolder.Returns(Environment.CurrentDirectory);
            context.InputFolder.Returns(inputFolder);
            context.Trace.Returns(trace);
            Engine engine = new Engine();
            engine.Configure();
            context.Assemblies.Returns(engine.Assemblies);
            IDocument document = Substitute.For<IDocument>();
            document.GetStream().Returns(new MemoryStream(Encoding.UTF8.GetBytes(@"@{ ExecutionContext.Trace.Information(""Test""); }")));
            Razor razor = new Razor();

            // When
            razor.Execute(new[] { document }, context).ToList();  // Make sure to materialize the result list

            // Then
            ITrace receivedTrace = context.Received().Trace; // Need to assign to dummy var to keep compiler happy
            trace.Received().Information("Test");
        }
Example #11
0
        public void CanCloneWithNewValues()
        {
            // Given
            Engine engine = new Engine();
            Metadata metadata = new Metadata(engine);

            // When
            metadata = metadata.Clone(new [] { new KeyValuePair<string, object>("A", "a") });

            // Then
            Assert.AreEqual("a", metadata["A"]);
        }
Example #12
0
        public void ClonedMetadataDoesNotContainNewValues()
        {
            // Given
            Engine engine = new Engine();
            engine.Metadata["A"] = "a";
            Metadata metadata = new Metadata(engine);

            // When
            Metadata clone = metadata.Clone(new Dictionary<string, object> { { "B", "b" } });

            // Then
            Assert.IsFalse(metadata.ContainsKey("B"));
        }
Example #13
0
        public void CloneContainsPreviousValues()
        {
            // Given
            Engine engine = new Engine();
            engine.Metadata["A"] = "a";
            Metadata metadata = new Metadata(engine);

            // When
            Metadata clone = metadata.Clone(new Dictionary<string, object> { { "B", "b" } });

            // Then
            Assert.AreEqual("a", clone["A"]);
        }
Example #14
0
 public void ExecuteAllExamples(string example)
 {
     Engine engine = new Engine();
     engine.Trace.AddListener(new TestTraceListener());
     engine.RootFolder = example;
     engine.Config.Assemblies.LoadDirectory(TestContext.CurrentContext.TestDirectory);
     string config = Path.Combine(example, "config.wyam");
     if (File.Exists(config))
     {
         engine.Configure(File.ReadAllText(config));
     }
     engine.Execute();
 }
Example #15
0
        public void CloneContainsNewValues()
        {
            // Given
            Engine engine = new Engine();
            engine.Trace.AddListener(new TestTraceListener());
            engine.Metadata["A"] = "a";
            Metadata metadata = new Metadata(engine);

            // When
            Metadata clone = metadata.Clone(new Dictionary<string, object> { { "B", "b" } });

            // Then
            Assert.AreEqual("b", clone["B"]);
        }
Example #16
0
        public void CloneReplacesValue()
        {
            // Given
            Engine engine = new Engine();
            engine.Metadata["A"] = "a";
            Metadata metadata = new Metadata(engine);

            // When
            Metadata clone = metadata.Clone(new Dictionary<string, object> { { "A", "b" } });

            // Then
            Assert.AreEqual("a", metadata["A"]);
            Assert.AreEqual("b", clone["A"]);
        }
Example #17
0
        public void ContainsKeyReturnsTrueForValidValue()
        {
            // Given
            Engine engine = new Engine();
            engine.Trace.AddListener(new TestTraceListener());
            engine.Metadata["A"] = "a";
            Metadata metadata = new Metadata(engine);

            // When
            bool contains = metadata.ContainsKey("A");

            // Then
            Assert.IsTrue(contains);
        }
Example #18
0
 public void ExecuteAllExamples(string example)
 {
     Engine engine = new Engine
     {
         RootFolder = example
     };
     engine.Trace.AddListener(new TestTraceListener());
     string config = Path.Combine(example, "config.wyam");
     if (File.Exists(config))
     {
         engine.Configure(File.ReadAllText(config));
     }
     engine.Execute();
 }
Example #19
0
        public void IndexerNullKeyThrowsKeyNotFoundException()
        {
            // Given
            Engine engine = new Engine();
            engine.Trace.AddListener(new TestTraceListener());
            Metadata metadata = new Metadata(engine);

            // When
            TestDelegate test = () =>
            {
                object value = metadata[null];
            };

            // Then
            Assert.Throws<ArgumentNullException>(test);
        }
Example #20
0
        public void AlternateIgnorePrefix()
        {
            // Given
            Engine engine = new Engine();
            engine.InputFolder = @"TestFiles\Input\";
            ReadFiles readFiles = new ReadFiles(@"AlternateIgnorePrefix\*.cshtml");
            Razor razor = new Razor().IgnorePrefix("Ignore");
            engine.Pipelines.Add("Pipeline", readFiles, razor);

            // When
            engine.Execute();

            // Then
            Assert.AreEqual(1, engine.Documents.FromPipeline("Pipeline").Count());
            Assert.AreEqual(@"<p>This is a test</p>", engine.Documents.FromPipeline("Pipeline").First().Content);
        }
Example #21
0
        public void LoadLayoutFile()
        {
            // Given
            Engine engine = new Engine();
            engine.InputFolder = @"TestFiles\Input\";
            ReadFiles readFiles = new ReadFiles(@"Layout\Test.cshtml");
            Razor razor = new Razor();
            engine.Pipelines.Add("Pipeline", readFiles, razor);

            // When
            engine.Execute();

            // Then
            Assert.AreEqual(1, engine.Documents.FromPipeline("Pipeline").Count());
            Assert.AreEqual("LAYOUT\r\n\r\n<p>This is a test</p>", engine.Documents.FromPipeline("Pipeline").First().Content);
        }
Example #22
0
        public void IgnoresUnderscoresByDefault()
        {
            // Given
            Engine engine = new Engine();
            engine.InputFolder = @"TestFiles\Input\";
            ReadFiles readFiles = new ReadFiles(@"IgnoreUnderscores\*.cshtml");
            Razor razor = new Razor();
            Meta meta = new Meta("Content", (x, y) => x.Content);
            engine.Pipelines.Add("Pipeline", readFiles, razor, meta);

            // When
            engine.Execute();

            // Then
            Assert.AreEqual(1, engine.Documents.FromPipeline("Pipeline").Count());
            Assert.AreEqual("LAYOUT\r\n\r\n<p>This is a test</p>", engine.Documents.FromPipeline("Pipeline").First().String("Content"));
        }
Example #23
0
        public void AlternateViewStartPath()
        {
            // Given
            Engine engine = new Engine();
            engine.InputFolder = @"TestFiles\Input\";
            ReadFiles readFiles = new ReadFiles(@"AlternateViewStartPath\Test.cshtml");
            Razor razor = new Razor().WithViewStart(@"AlternateViewStart\_ViewStart.cshtml");
            Meta meta = new Meta("Content", (x, y) => x.Content);
            engine.Pipelines.Add("Pipeline", readFiles, razor, meta);

            // When
            engine.Execute();

            // Then
            Assert.AreEqual(1, engine.Documents.FromPipeline("Pipeline").Count());
            Assert.AreEqual("LAYOUT\r\n<p>This is a test</p>", engine.Documents.FromPipeline("Pipeline").First().String("Content"));
        }
Example #24
0
 public Pipeline(string name, Engine engine, IModule[] modules)
 {
     if (string.IsNullOrWhiteSpace(name))
     {
         throw new ArgumentException("name");
     }
     if (engine == null)
     {
         throw new ArgumentNullException("engine");
     }
     _name = name;
     _engine = engine;
     if (modules != null)
     {
         _modules.AddRange(modules);
     }
 }
Example #25
0
        public void ConfigureSupportsGlobalConstructorMethods()
        {
            // Given
            Engine engine = new Engine();
            string configScript = @"
                Pipelines.Add(
                    ReadFiles(""*.cshtml""),
	                WriteFiles("".html""));
            ";

            // When
            engine.Configure(configScript);

            // Then
            Assert.AreEqual(1, ((PipelineCollection)engine.Pipelines).Pipelines.Count());
            Assert.AreEqual(2, ((PipelineCollection)engine.Pipelines).Pipelines.First().Count);
        }
Example #26
0
        public void ConfigureAddsPipelineAndModules()
        {
            // Given
            Engine engine = new Engine();
            string configScript = @"
                Pipelines.Add(
                    new ReadFiles(""*.cshtml""),
	                new WriteFiles("".html""));
            ";

            // When
            engine.Configure(configScript);

            // Then
            Assert.AreEqual(1, ((PipelineCollection)engine.Pipelines).Pipelines.Count());
            Assert.AreEqual(2, ((PipelineCollection)engine.Pipelines).Pipelines.First().Count);
        }
Example #27
0
            public void AlternateIgnorePrefix()
            {
                // Given
                Engine engine = new Engine();
                engine.RootFolder = TestContext.CurrentContext.TestDirectory;
                engine.InputFolder = @"TestFiles\Input\";
                ReadFiles readFiles = new ReadFiles(@"AlternateIgnorePrefix\*.cshtml");
                Razor razor = new Razor().IgnorePrefix("Ignore");
                Meta meta = new Meta("Content", (x, y) => x.Content);
                engine.Pipelines.Add("Pipeline", readFiles, razor, meta);

                // When
                engine.Execute();

                // Then
                Assert.AreEqual(1, engine.Documents.FromPipeline("Pipeline").Count());
                Assert.AreEqual(@"<p>This is a test</p>", engine.Documents.FromPipeline("Pipeline").First().String("Content"));
            }
Example #28
0
        public void ConfigureSetsPrimitiveMetadata()
        {
            // Given
            Engine engine = new Engine();
            string configScript = @"
                Metadata[""TestString""] = ""teststring"";
                Metadata[""TestInt""] = 1234;
                Metadata[""TestFloat""] = 1234.567;
                Metadata[""TestBool""] = true;
            ";

            // When
            engine.Configure(configScript);

            // Then
            Assert.AreEqual("teststring", engine.Metadata["TestString"]);
            Assert.AreEqual(1234, engine.Metadata["TestInt"]);
            Assert.AreEqual(1234.567, engine.Metadata["TestFloat"]);
            Assert.AreEqual(true, engine.Metadata["TestBool"]);
        }
        public void ToLookupWithValuesReturnsCorrectLookup()
        {
            // Given
            Engine engine = new Engine();
            Pipeline pipeline = new Pipeline("Pipeline", engine, null);
            IDocument a = new Document(engine, pipeline)
                .Clone("a", new[]
                {
                    new KeyValuePair<string, object>("Numbers", new[] { 1, 2, 3 }),
                    new KeyValuePair<string, object>("Colors", "Red") 
                });
            IDocument b = new Document(engine, pipeline)
                .Clone("b", new[]
                {
                    new KeyValuePair<string, object>("Numbers", new[] { 2, 3, 4 }),
                    new KeyValuePair<string, object>("Colors", new [] { "Red", "Blue" })
                });
            IDocument c = new Document(engine, pipeline)
                .Clone("c", new[]
                {
                    new KeyValuePair<string, object>("Numbers", 3),
                    new KeyValuePair<string, object>("Colors", "Green")
                });
            IDocument d = new Document(engine, pipeline)
                .Clone("d", new[]
                {
                    new KeyValuePair<string, object>("Numbers", "4"),
                    new KeyValuePair<string, object>("Colors", new [] { "Green", "Blue" })
                });
            List<IDocument> documents = new List<IDocument>() { a, b, c, d };

            // When
            ILookup<int, string> lookup = documents.ToLookup<int, string>("Numbers", "Colors");

            // Then
            Assert.AreEqual(4, lookup.Count);
            CollectionAssert.AreEquivalent(new[] { "Red" }, lookup[1]);
            CollectionAssert.AreEquivalent(new[] { "Red", "Blue" }, lookup[2]);
            CollectionAssert.AreEquivalent(new[] { "Red", "Blue", "Green" }, lookup[3]);
            CollectionAssert.AreEquivalent(new[] { "Red", "Blue", "Green" }, lookup[4]);
        }
Example #30
0
        public void CompletedMetadataIsPopulatedAfterRun()
        {
            // Given
            Engine engine = new Engine();
            engine.CleanOutputPathOnExecute = false;
            int c = 0;
            engine.Pipelines.Add("Pipeline",
                new Execute((x, ctx) => new[]
                {
                    ctx.GetDocument(x, (string)null, new Dictionary<string, object> { { c.ToString(), c++ } }),
                    ctx.GetDocument(x, (string)null, new Dictionary<string, object> { { c.ToString(), c++ } })
                }),
                new Execute((x, ctx) => new[]
                {
                    ctx.GetDocument(x, (string)null, new Dictionary<string, object> { { c.ToString(), c++ } })
                }));

            // When
            engine.Execute();

            // Then
            Assert.AreEqual(2, engine.Documents.FromPipeline("Pipeline").Count());

            Assert.IsTrue(engine.Documents.FromPipeline("Pipeline").First().Metadata.ContainsKey("0"));
            Assert.AreEqual(0, engine.Documents.FromPipeline("Pipeline").First().Metadata["0"]);
            Assert.IsTrue(engine.Documents.FromPipeline("Pipeline").First().Metadata.ContainsKey("2"));
            Assert.AreEqual(2, engine.Documents.FromPipeline("Pipeline").First().Metadata["2"]);
            Assert.IsFalse(engine.Documents.FromPipeline("Pipeline").First().Metadata.ContainsKey("1"));
            Assert.IsFalse(engine.Documents.FromPipeline("Pipeline").First().Metadata.ContainsKey("3"));

            Assert.IsTrue(engine.Documents.FromPipeline("Pipeline").Skip(1).First().Metadata.ContainsKey("1"));
            Assert.AreEqual(1, engine.Documents.FromPipeline("Pipeline").Skip(1).First().Metadata["1"]);
            Assert.IsTrue(engine.Documents.FromPipeline("Pipeline").Skip(1).First().Metadata.ContainsKey("3"));
            Assert.AreEqual(3, engine.Documents.FromPipeline("Pipeline").Skip(1).First().Metadata["3"]);
            Assert.IsFalse(engine.Documents.FromPipeline("Pipeline").Skip(1).First().Metadata.ContainsKey("0"));
            Assert.IsFalse(engine.Documents.FromPipeline("Pipeline").Skip(1).First().Metadata.ContainsKey("2"));
        }