From 17c52bd0aebfb05779c552f58442cea32bb12231 Mon Sep 17 00:00:00 2001 From: suhasgumma Date: Sat, 10 Dec 2022 18:24:06 +0530 Subject: [PATCH] Handle Single Line Sequence Node in Replacement --- core/pkg/fixhandler/fixhandler_test.go | 4 +-- core/pkg/fixhandler/yamlhandler.go | 27 +++++++++++++++++++ .../fixed_yaml_scenario_1.yml | 2 +- .../fixed_yaml_scenario_1.yml | 2 +- .../fixed_yaml_scenario_2.yml | 5 ++-- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/core/pkg/fixhandler/fixhandler_test.go b/core/pkg/fixhandler/fixhandler_test.go index cb54a415..d5a2e76c 100644 --- a/core/pkg/fixhandler/fixhandler_test.go +++ b/core/pkg/fixhandler/fixhandler_test.go @@ -143,10 +143,10 @@ func TestFixHandler_applyFixToFile(t *testing.T) { testDirectoryApplyFix(t, "remove_scenarios") // Tests for Replace scenarios - // testDirectoryApplyFix(t, "replace_scenarios") + testDirectoryApplyFix(t, "replace_scenarios") // Tests for Hybrid Scenarios - // testDirectoryApplyFix(t, "hybrid_scenarios") + testDirectoryApplyFix(t, "hybrid_scenarios") } diff --git a/core/pkg/fixhandler/yamlhandler.go b/core/pkg/fixhandler/yamlhandler.go index 3ed45e27..d4c608fe 100644 --- a/core/pkg/fixhandler/yamlhandler.go +++ b/core/pkg/fixhandler/yamlhandler.go @@ -234,6 +234,33 @@ func addLinesToInsert(fixInfoMetadata *FixInfoMetadata) (int, int) { func updateLinesToReplace(fixInfoMetadata *FixInfoMetadata) (int, int) { currentDFSNode := (*fixInfoMetadata.fixedList)[fixInfoMetadata.fixedListTracker] + isOneLine, line := isOneLineSequenceNode(fixInfoMetadata.fixedList, fixInfoMetadata.fixedListTracker) + + if isOneLine { + originalListTracker := getFirstNodeInLine(fixInfoMetadata.originalList, line) + fixedListTracker := getFirstNodeInLine(fixInfoMetadata.fixedList, line) + + currentDFSNode = (*fixInfoMetadata.fixedList)[fixedListTracker] + content := getContent(currentDFSNode.parent, fixInfoMetadata.fixedList, fixedListTracker) + + // Remove the Single line + *fixInfoMetadata.contentToRemove = append(*fixInfoMetadata.contentToRemove, ContentToRemove{ + startLine: line, + endLine: line, + }) + + // Encode entire Sequence Node and Insert + *fixInfoMetadata.contentToAdd = append(*fixInfoMetadata.contentToAdd, ContentToAdd{ + Line: line, + Content: content, + }) + + originalListTracker = updateTracker(fixInfoMetadata.originalList, originalListTracker) + fixedListTracker = updateTracker(fixInfoMetadata.fixedList, fixedListTracker) + + return originalListTracker, fixedListTracker + } + if isValueNodeinMapping(¤tDFSNode) { fixInfoMetadata.originalListTracker -= 1 fixInfoMetadata.fixedListTracker -= 1 diff --git a/examples/fix-command/hybrid_scenarios/fixed_yaml_scenario_1.yml b/examples/fix-command/hybrid_scenarios/fixed_yaml_scenario_1.yml index 5ab9f08c..b74c6cdc 100644 --- a/examples/fix-command/hybrid_scenarios/fixed_yaml_scenario_1.yml +++ b/examples/fix-command/hybrid_scenarios/fixed_yaml_scenario_1.yml @@ -16,4 +16,4 @@ spec: - name: nginx_container image: nginx securityContext: - runAsRoot: false \ No newline at end of file + runAsRoot: false diff --git a/examples/fix-command/replace_scenarios/fixed_yaml_scenario_1.yml b/examples/fix-command/replace_scenarios/fixed_yaml_scenario_1.yml index 4a4eda33..067565ca 100644 --- a/examples/fix-command/replace_scenarios/fixed_yaml_scenario_1.yml +++ b/examples/fix-command/replace_scenarios/fixed_yaml_scenario_1.yml @@ -11,4 +11,4 @@ spec: - name: nginx_container image: nginx securityContext: - runAsRoot: false \ No newline at end of file + runAsRoot: false diff --git a/examples/fix-command/replace_scenarios/fixed_yaml_scenario_2.yml b/examples/fix-command/replace_scenarios/fixed_yaml_scenario_2.yml index 89775e04..2c8b1da4 100644 --- a/examples/fix-command/replace_scenarios/fixed_yaml_scenario_2.yml +++ b/examples/fix-command/replace_scenarios/fixed_yaml_scenario_2.yml @@ -14,6 +14,5 @@ spec: securityContext: capabilities: drop: - - "SYS_ADM" - add: - - "NET_RAW" \ No newline at end of file + - "SYS_ADM" + add: ["NET_RAW"]