Ejemplo n.º 1
0
        public void AddSectionWorks()
        {
            EditorConfigFile editorConfigFile = PrepareTest("addsection", out var file, out var workingFile);

            IniSectionData iniSectionData = new IniSectionData("*.cs")
            {
                new IniCommentData("TEST ADDED SECTION"),
                new IniPropertyData("testProperty", "testValue"),
                new IniEmptyLine(),
                new IniCommentData("ANOTHER COMMENT"),
                new IniPropertyData("anotherProperty", "anotherValue")
            };

            var sectionLength = iniSectionData.Length;

            using (var editContext = editorConfigFile.Edit(new EditorConfigFileOptions {
                EndSectionWithBlankLineOrComment = false
            }))
            {
                editContext.AddSection(iniSectionData);

                editContext.SaveChanges();
            }

            editorConfigFile.Sections.Count.Should().Be(2);

            // Confirm the file is one line longer
            string[] fileText   = File.ReadAllLines(file);
            var      fileLength = fileText.Length;

            string[] workingFileText   = File.ReadAllLines(workingFile);
            var      workingFileLength = workingFileText.Length;

            workingFileLength.Should().Be(fileLength + sectionLength);
        }
Ejemplo n.º 2
0
        public void RemoveWorks()
        {
            EditorConfigFile editorConfigFile = PrepareTest("remove", out var file, out var workingFile);

            const int TestSection = 0;
            var       sectionName = editorConfigFile.Sections[TestSection].Name;

            // Find aftercomment property line
            editorConfigFile.TryGetProperty("aftercomment", editorConfigFile.Sections[TestSection], out var afterProp).Should().BeTrue();
            var lineNumber = afterProp !.LineNumber;

            // Remove beforecommment
            editorConfigFile.TryGetProperty("beforecomment", editorConfigFile.Sections[TestSection], out var prop).Should().BeTrue();
            using (var editContext = editorConfigFile.Edit(new EditorConfigFileOptions {
                EndSectionWithBlankLineOrComment = false
            }))
            {
                editContext.Sections[sectionName].Remove(prop !).Should().BeTrue();

                editContext.SaveChanges();
            }

            editorConfigFile.TryGetProperty("beforecomment", editorConfigFile.Sections[TestSection], out _).Should().BeFalse();

            // Confirm aftercomment has moved up a line
            editorConfigFile.TryGetProperty("aftercomment", editorConfigFile.Sections[TestSection], out var updatedProperty).Should().BeTrue();
            updatedProperty !.LineNumber.Should().Be(lineNumber - 1);

            // Confirm the file is one line shorter
            var fileLength        = File.ReadAllLines(file).Length;
            var workingFileLength = File.ReadAllLines(workingFile).Length;

            workingFileLength.Should().Be(fileLength - 1);
        }
Ejemplo n.º 3
0
        public void RemoveSectionWorks()
        {
            EditorConfigFile editorConfigFile = PrepareTest("removesection", out var file, out var workingFile);

            const int TestSection = 0;

            // Find number of lines in section
            var sectionLength = editorConfigFile.Sections[TestSection].Length;
            var sectionName   = editorConfigFile.Sections[TestSection].Name;

            using (var editContext = editorConfigFile.Edit())
            {
                editContext.Sections.Remove(sectionName);

                editContext.SaveChanges();
            }

            editorConfigFile.Sections.Count.Should().Be(0);

            // Confirm the file is shorter by the number of removed lines
            var fileLength        = File.ReadAllLines(file).Length;
            var workingFileLength = File.ReadAllLines(workingFile).Length;

            // The final line would have been an empty line, which gets removed
            workingFileLength.Should().Be(fileLength - sectionLength - 1);
        }
        public void NonexistentGlobalCommentIsNotFound()
        {
            var file = GetFileFromMethod(MethodBase.GetCurrentMethod(), FileName);
            EditorConfigFile editorConfigFile = new EditorConfigFile(file);

            editorConfigFile.TryGetComment("Not a real comment", editorConfigFile.Global, out var comment).Should().BeFalse();

            comment.Should().BeNull();
        }
        public void ExistingGlobalCommentIsFound()
        {
            var file = GetFileFromMethod(MethodBase.GetCurrentMethod(), FileName);
            EditorConfigFile editorConfigFile = new EditorConfigFile(file);

            editorConfigFile.TryGetComment(GlobalCommentText, editorConfigFile.Global, out var comment).Should().BeTrue();

            comment !.Data.Text.Should().Be(GlobalCommentText);
            comment.LineNumber.Should().Be(3);
        }
        public void ExistingSectionCommentIsFound()
        {
            var file = GetFileFromMethod(MethodBase.GetCurrentMethod(), FileName);
            EditorConfigFile editorConfigFile = new EditorConfigFile(file);

            var section = editorConfigFile.Sections[0];

            editorConfigFile.TryGetComment(SectionCommentText, section, out var comment).Should().BeTrue();

            comment !.Data.Text.Should().Be(SectionCommentText);
            comment.LineNumber.Should().Be(7);
        }
Ejemplo n.º 7
0
    // Gets profile configurations and alters the file name.
    IList <EditorConfigFile> GetProfileConfigurations(ref string fileName)
    {
        var root = Manager.GetFolderPath(SpecialFolder.RoamingData, false);
        var path = Path.Combine(root, ".editorconfig");

        if (!File.Exists(path))
        {
            return(null);
        }

        fileName = Path.Combine(root, Path.GetFileName(fileName));

        var configFile = new EditorConfigFile(path);

        return(new EditorConfigFile[] { configFile });
    }
Ejemplo n.º 8
0
        public void NotSavingMakesNoChanges()
        {
            EditorConfigFile editorConfigFile = PrepareTest("nochange", out string file, out string workingFile);

            const int    TestSection       = 0;
            const string TestPropertyKey   = "testProperty";
            const string TestPropertyValue = "testValue";

            var iniProperty = new IniPropertyData(TestPropertyKey, TestPropertyValue);

            var sectionName = editorConfigFile.Sections[TestSection].Name;

            using (var editContext = editorConfigFile.Edit())
            {
                editContext.Sections[sectionName].Add(iniProperty);
            }

            var originalText = File.ReadAllText(file);
            var copyText     = File.ReadAllText(workingFile);

            originalText.Should().Be(copyText);
        }
Ejemplo n.º 9
0
        public void AddWorks()
        {
            EditorConfigFile editorConfigFile = PrepareTest("add", out var file, out var workingFile);

            const int    TestSection       = 0;
            const string TestPropertyKey   = "testProperty";
            const string TestPropertyValue = "testValue";

            var section     = editorConfigFile.Sections[TestSection];
            var sectionName = section.Name;

            // Find aftercomment property line
            editorConfigFile.TryGetProperty("aftercomment", section, out var prop).Should().BeTrue();
            var lineNumber = prop !.LineNumber;

            var iniProperty = new IniPropertyData(TestPropertyKey, TestPropertyValue);

            using (var editContext = editorConfigFile.Edit(new EditorConfigFileOptions {
                EndSectionWithBlankLineOrComment = false
            }))
            {
                editContext.Sections[sectionName].Add(iniProperty);

                editContext.SaveChanges();
            }

            editorConfigFile.TryGetProperty(TestPropertyKey, editorConfigFile.Sections[TestSection], out var updatedProperty).Should().BeTrue();
            updatedProperty !.Data.Value.Should().Be(TestPropertyValue);

            // Use the original value rather than re-reading the property. This ensures that the file is otherwise unchanged
            updatedProperty !.LineNumber.Should().Be(lineNumber + 1);

            // Confirm the file is one line longer
            var fileLength        = File.ReadAllLines(file).Length;
            var workingFileLength = File.ReadAllLines(workingFile).Length;

            workingFileLength.Should().Be(fileLength + 1);
        }
 protected override bool OnAnalyse()
 {
     editorConfigFile = new EditorConfigFile(File.ReadLines(editorConfigPath));
     return(editorConfigFile.Preamble.Declarations.Any(d => d.Key == "root" && d.Value == "true"));
 }
        public void File_parses_correctly()
        {
            EditorConfigSection           section;
            EditorConfigLine              line;
            EditorConfigSectionHeaderLine headerLine;
            EditorConfigCommentLine       commentLine;
            EditorConfigBlankLine         blankLine;
            EditorConfigDeclarationLine   declarationLine;

            var file = new EditorConfigFile(lines);

            Assert.AreEqual(3, file.Sections.Count);

            section = file.Sections[0];
            Assert.IsTrue(section.IsPreamble);
            Assert.AreEqual(5, section.Lines.Count);

            line = section.Lines[0];
            Assert.AreEqual(1, line.LineNumber);
            Assert.IsNotNull(commentLine = line as EditorConfigCommentLine);
            Assert.AreEqual("comment_preamble1", commentLine.Comment);

            line = section.Lines[1];
            Assert.AreEqual(2, line.LineNumber);
            Assert.IsNotNull(blankLine = line as EditorConfigBlankLine);

            line = section.Lines[2];
            Assert.AreEqual(3, line.LineNumber);
            Assert.IsNotNull(commentLine = line as EditorConfigCommentLine);
            Assert.AreEqual("comment_preamble2", commentLine.Comment);

            line = section.Lines[3];
            Assert.AreEqual(4, line.LineNumber);
            Assert.IsNotNull(declarationLine = line as EditorConfigDeclarationLine);
            Assert.AreEqual("key_preamble1", declarationLine.Key);
            Assert.AreEqual("value_preamble1", declarationLine.Value);

            line = section.Lines[4];
            Assert.AreEqual(5, line.LineNumber);
            Assert.IsNotNull(blankLine = line as EditorConfigBlankLine);

            section = file.Sections[1];
            Assert.IsFalse(section.IsPreamble);
            Assert.AreEqual(4, section.Lines.Count);

            line = section.Lines[0];
            Assert.AreEqual(6, line.LineNumber);
            Assert.IsNotNull(headerLine = line as EditorConfigSectionHeaderLine);
            Assert.AreEqual("a", headerLine.Name);

            line = section.Lines[1];
            Assert.AreEqual(7, line.LineNumber);
            Assert.IsNotNull(declarationLine = line as EditorConfigDeclarationLine);
            Assert.AreEqual("key_a1", declarationLine.Key);
            Assert.AreEqual("value_a1", declarationLine.Value);

            line = section.Lines[2];
            Assert.AreEqual(8, line.LineNumber);
            Assert.IsNotNull(declarationLine = line as EditorConfigDeclarationLine);
            Assert.AreEqual("key_a2", declarationLine.Key);
            Assert.AreEqual("value_a2", declarationLine.Value);

            line = section.Lines[3];
            Assert.AreEqual(9, line.LineNumber);
            Assert.IsNotNull(blankLine = line as EditorConfigBlankLine);

            section = file.Sections[2];
            Assert.IsFalse(section.IsPreamble);
            Assert.AreEqual(2, section.Lines.Count);

            line = section.Lines[0];
            Assert.AreEqual(10, line.LineNumber);
            Assert.IsNotNull(headerLine = line as EditorConfigSectionHeaderLine);
            Assert.AreEqual("b", headerLine.Name);

            line = section.Lines[1];
            Assert.AreEqual(11, line.LineNumber);
            Assert.IsNotNull(declarationLine = line as EditorConfigDeclarationLine);
            Assert.AreEqual("key_b1", declarationLine.Key);
            Assert.AreEqual("value_b1", declarationLine.Value);
        }
Ejemplo n.º 12
0
 protected override bool OnAnalyse()
 {
     editorConfigFile = new EditorConfigFile(File.ReadLines(editorConfigPath));
     section          = editorConfigFile.Sections.Single(s => s.Header?.Name == sectionName);
     return(section.Declarations.Any(d => d.Key == key && d.Value == value));
 }
Ejemplo n.º 13
0
 protected override bool OnAnalyse()
 {
     editorConfigFile = new EditorConfigFile(File.ReadLines(editorConfigPath));
     return(editorConfigFile.Sections.Any(s => s.Header?.Name == name));
 }