Uploaded image for project: 'CFEngine Community'
  1. CFEngine Community
  2. CFE-3988

edit_line doesn't insert after the last matched line when selecting a region where the end delimiter is included

    XMLWordPrintable

    Details

      Description

      Similar to CFE-3987, I am trying to edit a file and insert text after a multi-line pattern. Since location.select_line_matching is unable to match against multiple lines (ENT-8719) I use a select_region body to select the multi-line pattern and specify that content should be edited after the last matching line. I expected that content would be inserted after the end of the selected region but text is inserted before the end of the region. It sort of feel like include_end_delimiter => "true" is not taking effect.

      Reproducer

      Example Policy
      bundle agent __main__
      {
        methods:
            "init";
            "test";
            "check";
        reports:
            "/tmp/example.xml"
              printfile => my_cat($(this.promiser));
      }
      bundle edit_line ENT_8791
      {
        insert_lines:
            "INSERT ME"
              select_region => my_comment_last_filter,
              location => my_location_after;
      }
      body location my_location_after
      # @brief Editing occurs before the matched line
      {
              before_after => "after"; # Edit after
              first_last => "last";    # The last matching line
      
              select_line_matching => ".*"; # Since select_region.include_end_delimiter
            # is set to true, I expected text to be
            # edited immediately after the line ending
            # the region.
      }
      
      body select_region my_comment_last_filter
      {
              select_start => "\s+THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN";
              select_end => "\s+=+\s+-->";
              include_start_delimiter => "true";
              include_end_delimiter => "true";
              select_end_match_eof => "false";
      }
      
      bundle agent test
      {
        files:
            "/tmp/example.xml"
              edit_line => ENT_8791;
      }
      
      bundle agent check
      {
        classes:
      
            # We expect to find text that looks like this:
            #           THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN
            #           ===================================================== -->
            #INSERT ME
      
            "found_expected_pattern"
              expression => regcmp( ".*\s+THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN\R\s+=+\s+-->\RINSERT\sME.*",
                                    readfile( "/tmp/example.xml" ) );
      
        reports:
          found_expected_pattern::
            "Pass $(this.promise_filename)";
          !found_expected_pattern::
            "FAIL $(this.promise_filename)";
      
      }
      bundle agent init
      {
        files:
            "/tmp/example.xml"
              content => `<?xml version="1.0"?>
            <web-app xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
            version="3.0"
            metadata-complete="true">
            <!-- General -->
            <display-name>Atlassian JIRA Web Application</display-name>
            <description>The Atlassian JIRA web application - see http://www.atlassian.com/software/jira for more information
            </description>
      
            <absolute-ordering />
      
            <!-- Filters -->
      
            <!-- Special filters that must come at the beginning of the chain because they prevent
            all other filters from running.  This is to prevent those later filters from doing
            lookups in Pico, which could alter the order in which it instantiates components
            and thereby trigger a deadlock. -->
      
            <filter>
            <filter-name>JiraImportProgressFilter</filter-name>
            <filter-class&amp;gt;com.atlassian.jira.web.filters.JiraImportProgressFilter</filter-class&amp;gt;
            </filter>
      
            <!-- ========================================================
            THIS MUST BE THE FIRST FILTER IN THE NORMAL FILTER CHAIN
            ======================================================== -->
      
            <filter>
            <filter-name>JiraFirstFilter</filter-name>
            <filter-class&amp;gt;com.atlassian.jira.web.filters.JiraFirstFilter</filter-class&amp;gt;
            </filter>
      
            <!-- =====================================================
            THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN
            ===================================================== -->
      
            <filter>
            <filter-name>JiraLastFilter</filter-name>
            <filter-class&amp;gt;com.atlassian.jira.web.filters.JiraLastFilter</filter-class&amp;gt;
            </filter>
      
            <!-- =====================================================
            FILTER MAPPINGS FOLLOW :
            ===================================================== -->
      
            </web-app>`;
      
      }
      body printfile my_cat(file)
      # @brief Report the contents of a file
      # @param file The full path of the file to report
      {
              file_to_print => "$(file)";
              number_of_lines => "inf";
      }
      
          info: Updated file '/tmp/example.xml' with content '<?xml version="1.0"?>
            <web-app xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
            version="3.0"
            metadata-complete="true">
            <!-- General -->
            <display-name>Atlassian JIRA Web Application</display-name>
            <description>The Atlassian JIRA web application - see http://www.atlassian.com/software/jira for more information
            </description>
      
            <absolute-ordering />
      
            <!-- Filters -->
      
            <!-- Special filters that must come at the beginning of the chain because they prevent
            all other filters from running.  This is to prevent those later filters from doing
            lookups in Pico, which could alter the order in which it instantiates components
            and thereby trigger a deadlock. -->
      
            <filter>
            <filter-name>JiraImportProgressFilter</filter-name>
            <filter-class&gt;com.atlassian.jira.web.filters.JiraImportProgressFilter</filter-class&gt;
            </filter>
      
            <!-- ========================================================
            THIS MUST BE THE FIRST FILTER IN THE NORMAL FILTER CHAIN
            ======================================================== -->
      
            <filter>
            <filter-name>JiraFirstFilter</filter-name>
            <filter-class&gt;com.atlassian.jira.web.filters.JiraFirstFilter</filter-class&gt;
            </filter>
      
            <!-- =====================================================
            THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN
            ===================================================== -->
      
            <filter>
            <filter-name>JiraLastFilter</filter-name>
            <filter-class&gt;com.atlassian.jira.web.filters.JiraLastFilter</filter-class&gt;
            </filter>
      
            <!-- =====================================================
            FILTER MAPPINGS FOLLOW :
            ===================================================== -->
      
            </web-app>'
          info: Inserted the promised line 'INSERT ME' into '/tmp/example.xml' after locator
          info: insert_lines promise 'INSERT ME' repaired
          info: Edited file '/tmp/example.xml'
      R: FAIL /home/nickanderson/org/roam/daily/work/cfengine3-ofxuqZ
      R: /tmp/example.xml
      R: <?xml version="1.0"?>
      R:       <web-app xmlns="http://java.sun.com/xml/ns/javaee"
      R:       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      R:       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      R:         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      R:       version="3.0"
      R:       metadata-complete="true">
      R:       <!-- General -->
      R:       <display-name>Atlassian JIRA Web Application</display-name>
      R:       <description>The Atlassian JIRA web application - see http://www.atlassian.com/software/jira for more information
      R:       </description>
      R: 
      R:       <absolute-ordering />
      R: 
      R:       <!-- Filters -->
      R: 
      R:       <!-- Special filters that must come at the beginning of the chain because they prevent
      R:       all other filters from running.  This is to prevent those later filters from doing
      R:       lookups in Pico, which could alter the order in which it instantiates components
      R:       and thereby trigger a deadlock. -->
      R: 
      R:       <filter>
      R:       <filter-name>JiraImportProgressFilter</filter-name>
      R:       <filter-class&gt;com.atlassian.jira.web.filters.JiraImportProgressFilter</filter-class&gt;
      R:       </filter>
      R: 
      R:       <!-- ========================================================
      R:       THIS MUST BE THE FIRST FILTER IN THE NORMAL FILTER CHAIN
      R:       ======================================================== -->
      R: 
      R:       <filter>
      R:       <filter-name>JiraFirstFilter</filter-name>
      R:       <filter-class&gt;com.atlassian.jira.web.filters.JiraFirstFilter</filter-class&gt;
      R:       </filter>
      R: 
      R:       <!-- =====================================================
      R:       THIS MUST BE THE LAST FILTER IN THE DEFINED CHAIN
      R: INSERT ME
      R:       ===================================================== -->
      R: 
      R:       <filter>
      R:       <filter-name>JiraLastFilter</filter-name>
      R:       <filter-class&gt;com.atlassian.jira.web.filters.JiraLastFilter</filter-class&gt;
      R:       </filter>
      R: 
      R:       <!-- =====================================================
      R:       FILTER MAPPINGS FOLLOW :
      R:       ===================================================== -->
      R: 
      R:       </web-app>
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              a10042 Nick Anderson
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: