Exemplo n.º 1
0
        public void OneAttributeTwoPositionalParamsWithSameValue()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute = new TaskItem("AssemblyMetadataAttribute");

            attribute.SetMetadata("_Parameter1", "TestValue");
            attribute.SetMetadata("_Parameter2", "TestValue");
            task.AssemblyAttributes = new TaskItem[] { attribute };
            task.Language           = "c#";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.True(result);

            string content = File.ReadAllText(task.OutputFile.ItemSpec);

            Console.WriteLine(content);

            CheckContentCSharp(content, @"[assembly: AssemblyMetadataAttribute(""TestValue"", ""TestValue"")]");

            File.Delete(task.OutputFile.ItemSpec);
        }
        public void OneAttributePositionalAndNamedParams()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute = new TaskItem("AssemblyTrademarkAttribute");

            attribute.SetMetadata("_Parameter1", "Microsoft");
            attribute.SetMetadata("Date", "2009");
            attribute.SetMetadata("Copyright", "(C)");
            task.AssemblyAttributes = new TaskItem[] { attribute };
            task.Language           = "c#";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.Equal(true, result);

            string content = File.ReadAllText(task.OutputFile.ItemSpec);

            Console.WriteLine(content);

            Assert.Equal(true, content.Contains(@"[assembly: AssemblyTrademarkAttribute(""Microsoft"", Date=""2009"", Copyright=""(C)"")]"));

            File.Delete(task.OutputFile.ItemSpec);
        }
Exemplo n.º 3
0
        public void MultilineAttributeCSharp()
        {
            var lines           = new[] { "line 1", "line 2", "line 3" };
            var multilineString = String.Join(Environment.NewLine, lines);

            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute = new TaskItem("System.Reflection.AssemblyDescriptionAttribute");

            attribute.SetMetadata("_Parameter1", multilineString);
            attribute.SetMetadata("Description", multilineString);
            task.AssemblyAttributes = new TaskItem[] { attribute };
            task.Language           = "c#";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.True(result);

            string content = File.ReadAllText(task.OutputFile.ItemSpec);

            Console.WriteLine(content);

            var csMultilineString = lines.Aggregate((l1, l2) => l1 + EscapedLineSeparator + l2);

            CheckContentCSharp(content, $"[assembly: System.Reflection.AssemblyDescriptionAttribute(\"{csMultilineString}\", Description=\"{csMultilineString}\")]");

            File.Delete(task.OutputFile.ItemSpec);
        }
Exemplo n.º 4
0
        public void TwoAttributes()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute1 = new TaskItem("AssemblyTrademarkAttribute");

            attribute1.SetMetadata("Name", "Microsoft");
            TaskItem attribute2 = new TaskItem("System.AssemblyCultureAttribute");

            attribute2.SetMetadata("Culture", "en-US");
            task.AssemblyAttributes = new TaskItem[] { attribute1, attribute2 };
            task.Language           = "c#";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.True(result);

            string content = File.ReadAllText(task.OutputFile.ItemSpec);

            Console.WriteLine(content);

            CheckContentCSharp(
                content,
                @"[assembly: AssemblyTrademarkAttribute(Name=""Microsoft"")]",
                @"[assembly: System.AssemblyCultureAttribute(Culture=""en-US"")]");
        }
Exemplo n.º 5
0
        public void OneAttributeTwoParams()
        {
            string file = Path.Combine(Path.GetTempPath(), "OneAttribute.tmp");

            try
            {
                WriteCodeFragment task   = new WriteCodeFragment();
                MockEngine        engine = new MockEngine(true);
                task.BuildEngine = engine;
                TaskItem attribute = new TaskItem("AssemblyTrademarkAttribute");
                attribute.SetMetadata("Company", "Microsoft");
                attribute.SetMetadata("Year", "2009");
                task.AssemblyAttributes = new TaskItem[] { attribute };
                task.Language           = "c#";
                task.OutputFile         = new TaskItem(file);
                bool result = task.Execute();

                Assert.True(result);
                Assert.True(File.Exists(file));

                string content = File.ReadAllText(file);
                Console.WriteLine(content);

                CheckContentCSharp(content, @"[assembly: AssemblyTrademarkAttribute(Company=""Microsoft"", Year=""2009"")]");
            }
            finally
            {
                File.Delete(file);
            }
        }
        public void OneAttributePositionalAndNamedParamsVisualBasic()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute = new TaskItem("AssemblyTrademarkAttribute");

            attribute.SetMetadata("_Parameter1", "Microsoft");
            attribute.SetMetadata("_Parameter2", "2009");
            attribute.SetMetadata("Copyright", "(C)");
            task.AssemblyAttributes = new TaskItem[] { attribute };
            task.Language           = "visualbasic";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.True(result);

            string content = File.ReadAllText(task.OutputFile.ItemSpec);

            Console.WriteLine(content);

            CheckContentVB(content, @"<Assembly: AssemblyTrademarkAttribute(""Microsoft"", ""2009"", Copyright:=""(C)"")>");

            File.Delete(task.OutputFile.ItemSpec);
        }
Exemplo n.º 7
0
        public void OneAttributeNoParams()
        {
            string file = Path.Combine(Path.GetTempPath(), "OneAttribute.tmp");

            try
            {
                WriteCodeFragment task   = new WriteCodeFragment();
                MockEngine        engine = new MockEngine(true);
                task.BuildEngine = engine;
                TaskItem attribute = new TaskItem("System.AssemblyTrademarkAttribute");
                task.AssemblyAttributes = new TaskItem[] { attribute };
                task.Language           = "c#";
                task.OutputFile         = new TaskItem(file);
                bool result = task.Execute();

                Assert.True(result);
                Assert.True(File.Exists(file));

                string content = File.ReadAllText(file);
                Console.WriteLine(content);

                CheckContentCSharp(content, "[assembly: System.AssemblyTrademarkAttribute()]");
            }
            finally
            {
                File.Delete(file);
            }
        }
        public void MultilineAttributeVB()
        {
            var lines           = new [] { "line 1", "line 2", "line 3" };
            var multilineString = String.Join(Environment.NewLine, lines);

            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute = new TaskItem("System.Reflection.AssemblyDescriptionAttribute");

            attribute.SetMetadata("_Parameter1", multilineString);
            attribute.SetMetadata("Description", multilineString);
            task.AssemblyAttributes = new TaskItem[] { attribute };
            task.Language           = "visualbasic";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.True(result);

            string content = File.ReadAllText(task.OutputFile.ItemSpec);

            Console.WriteLine(content);

            var vbMultilineString = lines
                                    .Select(l => $"\"{l}\"")
                                    .Aggregate((l1, l2) => $"{l1}&{VBLineSeparator}&{l2}");

            CheckContentVB(content, $"<Assembly: System.Reflection.AssemblyDescriptionAttribute({vbMultilineString}, Description:={vbMultilineString})>");

            File.Delete(task.OutputFile.ItemSpec);
        }
Exemplo n.º 9
0
        public void OneAttributePositionalAndNamedParams()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute = new TaskItem("AssemblyTrademarkAttribute");

            attribute.SetMetadata("_Parameter1", "Microsoft");
            attribute.SetMetadata("Date", "2009");
            attribute.SetMetadata("Copyright", "(C)");
            task.AssemblyAttributes = new TaskItem[] { attribute };
            task.Language           = "c#";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.True(result);

            string content = File.ReadAllText(task.OutputFile.ItemSpec);

            Console.WriteLine(content);

            // NOTE: order here is defined by dictionary traversal order and may change
            // based on implementation details there, but named parameters can have different
            // orders so that's ok.
            CheckContentCSharp(content, @"[assembly: AssemblyTrademarkAttribute(""Microsoft"", Copyright=""(C)"", Date=""2009"")]");

            File.Delete(task.OutputFile.ItemSpec);
        }
Exemplo n.º 10
0
        private void ExecuteAndVerifySuccess(WriteCodeFragment task, params string[] expectedAttributes)
        {
            MockEngine engine = new(true);

            task.BuildEngine = engine;

            try
            {
                var result = task.Execute();

                // Provide the log output as the user message so that the assertion failure
                // message is a bit more meaningful than just "Expected false to equal true".
                Assert.True(result, engine.Log);

                string content = File.ReadAllText(task.OutputFile.ItemSpec);
                Console.WriteLine(content);

                if (task.Language == "c#")
                {
                    CheckContentCSharp(content, expectedAttributes);
                }
                else
                {
                    CheckContentVB(content, expectedAttributes);
                }
            }
            finally
            {
                if ((task.OutputFile is not null) && File.Exists(task.OutputFile.ItemSpec))
                {
                    File.Delete(task.OutputFile.ItemSpec);
                }
            }
        }
        public void NoLanguage()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            task.OutputFile  = new TaskItem("foo");
            bool result = task.Execute();

            Assert.False(result);
            engine.AssertLogContains("MSB3098");
        }
        public void NoFileOrDirectory()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            task.Language    = "c#";
            bool result = task.Execute();

            Assert.False(result);
            engine.AssertLogContains("MSB3711");
        }
Exemplo n.º 13
0
        public void InvalidLanguage()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            task.Language    = "xx";
            task.OutputFile  = new TaskItem("foo");
            bool result = task.Execute();

            Assert.Equal(false, result);
            engine.AssertLogContains("MSB3712");
        }
        [PlatformSpecific(TestPlatforms.Windows)] // "No invalid characters on Unix"
        public void InvalidDirectoryPath()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine        = engine;
            task.Language           = "c#";
            task.AssemblyAttributes = new TaskItem[] { new TaskItem("aa") };
            task.OutputDirectory    = new TaskItem("||invalid||");
            bool result = task.Execute();

            Assert.False(result);
            engine.AssertLogContains("MSB3713");
        }
        public void NoNumber()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute = new TaskItem("AssemblyTrademarkAttribute");

            attribute.SetMetadata("_Parameter", "2009");
            task.AssemblyAttributes = new TaskItem[] { attribute };
            task.Language           = "c#";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.False(result);

            engine.AssertLogContains("MSB3098");
        }
Exemplo n.º 16
0
        public void OneAttributePositionalParamInvalidSuffix()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute = new TaskItem("AssemblyTrademarkAttribute");

            attribute.SetMetadata("_ParameterXXXXXXXXXX", "Microsoft");
            task.AssemblyAttributes = new TaskItem[] { attribute };
            task.Language           = "c#";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.Equal(false, result);

            engine.AssertLogContains("MSB3098");
        }
        public void CombineFileDirectory()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine        = engine;
            task.Language           = "c#";
            task.AssemblyAttributes = new TaskItem[] { new TaskItem("aa") };
            task.OutputFile         = new TaskItem("CombineFileDirectory.tmp");
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.True(result);

            string file = Path.Combine(Path.GetTempPath(), "CombineFileDirectory.tmp");

            Assert.Equal(file, task.OutputFile.ItemSpec);
            Assert.True(File.Exists(file));
        }
        public void OneAttributeTwoParamsSameName()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute = new TaskItem("AssemblyTrademarkAttribute");

            attribute.SetMetadata("Company", "Microsoft");
            attribute.SetMetadata("Company", "2009");
            task.AssemblyAttributes = new TaskItem[] { attribute };
            task.Language           = "c#";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.True(result);

            File.Delete(task.OutputFile.ItemSpec);
        }
        public void ToDirectory()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute = new TaskItem("System.AssemblyTrademarkAttribute");

            task.AssemblyAttributes = new TaskItem[] { attribute };
            task.Language           = "c#";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.True(result);
            Assert.True(File.Exists(task.OutputFile.ItemSpec));
            Assert.Equal(Path.GetTempPath(), task.OutputFile.ItemSpec.Substring(0, Path.GetTempPath().Length));
            Assert.Equal(".cs", task.OutputFile.ItemSpec.Substring(task.OutputFile.ItemSpec.Length - 3));

            File.Delete(task.OutputFile.ItemSpec);
        }
        public void OneAttributeNoParamsVb()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine = engine;
            TaskItem attribute = new TaskItem("System.AssemblyTrademarkAttribute");

            task.AssemblyAttributes = new TaskItem[] { attribute };
            task.Language           = "visualbasic";
            task.OutputDirectory    = new TaskItem(Path.GetTempPath());
            bool result = task.Execute();

            Assert.True(result);

            string content = File.ReadAllText(task.OutputFile.ItemSpec);

            Console.WriteLine(content);

            CheckContentVB(content, "<Assembly: System.AssemblyTrademarkAttribute()>");
        }
Exemplo n.º 21
0
        public void NoAttributesShouldEmitNoFile()
        {
            string file = Path.Combine(Path.GetTempPath(), "NoAttributesShouldEmitNoFile.tmp");

            if (File.Exists(file))
            {
                File.Delete(file);
            }

            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine        = engine;
            task.Language           = "c#";
            task.AssemblyAttributes = new TaskItem[] { }; // MSBuild sets an empty array
            task.OutputFile         = new TaskItem(file);
            bool result = task.Execute();

            Assert.Equal(true, result);
            Assert.Equal(false, File.Exists(file));
            Assert.Equal(null, task.OutputFile);
        }
        public void NoAttributesShouldEmitNoFile2()
        {
            string file = Path.Combine(Path.GetTempPath(), "NoAttributesShouldEmitNoFile.tmp");

            if (File.Exists(file))
            {
                File.Delete(file);
            }

            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine        = engine;
            task.Language           = "c#";
            task.AssemblyAttributes = null; // null this time
            task.OutputFile         = new TaskItem(file);
            bool result = task.Execute();

            Assert.True(result);
            Assert.False(File.Exists(file));
            Assert.Null(task.OutputFile);
        }
Exemplo n.º 23
0
        public void DirectoryAndRootedFile()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine        = engine;
            task.Language           = "c#";
            task.AssemblyAttributes = new TaskItem[] { new TaskItem("aa") };

            string folder = Path.Combine(Path.GetTempPath(), "foo\\");
            string file   = Path.Combine(folder, "CombineFileDirectory.tmp");

            Directory.CreateDirectory(folder);
            task.OutputFile      = new TaskItem(file);
            task.OutputDirectory = new TaskItem("c:\\");
            bool result = task.Execute();

            Assert.AreEqual(true, result);

            Assert.AreEqual(file, task.OutputFile.ItemSpec);
            Assert.AreEqual(true, File.Exists(file));

            Directory.Delete(folder, true);
        }
Exemplo n.º 24
0
        private void ExecuteAndVerifyFailure(WriteCodeFragment task, string errorCode)
        {
            MockEngine engine = new(true);

            task.BuildEngine = engine;

            try
            {
                var result = task.Execute();

                // Provide the log output as the user message so that the assertion failure
                // message is a bit more meaningful than just "Expected true to equal false".
                Assert.False(result, engine.Log);

                engine.AssertLogContains(errorCode);
            }
            finally
            {
                if ((task.OutputFile is not null) && File.Exists(task.OutputFile.ItemSpec))
                {
                    File.Delete(task.OutputFile.ItemSpec);
                }
            }
        }
        public void DirectoryAndRootedFile()
        {
            WriteCodeFragment task   = new WriteCodeFragment();
            MockEngine        engine = new MockEngine(true);

            task.BuildEngine        = engine;
            task.Language           = "c#";
            task.AssemblyAttributes = new TaskItem[] { new TaskItem("aa") };

            string folder = Path.Combine(Path.GetTempPath(), "foo" + Path.DirectorySeparatorChar);
            string file   = Path.Combine(folder, "CombineFileDirectory.tmp");

            Directory.CreateDirectory(folder);
            task.OutputFile      = new TaskItem(file);
            task.OutputDirectory = new TaskItem("c:\\");
            bool result = task.Execute();

            Assert.True(result);

            Assert.Equal(file, task.OutputFile.ItemSpec);
            Assert.True(File.Exists(file));

            FileUtilities.DeleteWithoutTrailingBackslash(folder, true);
        }