Skip to content

Commit

Permalink
Fix flag extraction bug in extract_desugar_pgcfg_flags
Browse files Browse the repository at this point in the history
Previous implementation would just blindly split the shrinker_config field as a string by spaces. This logic is incorrect. The right thing to do is to tell the program what format the data is coming in as (i.e. define the struct for the shrinker config flags), and unmarshall the JSON bytes with that in mind.

This CL also enhances the test for this tool to better reflect the actual intended logic.

Part of #122

PiperOrigin-RevId: 601239403
Change-Id: Id4d3e5b7daa92f3d07d588894234f7a2add41f1e
  • Loading branch information
ted-xie authored and Copybara-Service committed Jan 24, 2024
1 parent f53c8c9 commit 15818b2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -57,15 +56,15 @@ func main() {

// The r8 desugar config json schema is pretty complicated (+subject to change), and we only
// need one field, so instead of reading into a predefined data structure, we just read into a
// map[string]any to keep things simple.
var result map[string]any
// simple struct containing only the relevant shrinker_config string[] field.
type shrinkerConfig struct {
ShrinkerConfigFlags []string `json:"shrinker_config"`
}

var result shrinkerConfig
json.Unmarshal(jsonBytes, &result)

// Read the shrinker_config field as a string, then trim the demarcating [ ] characters.
shrinkerConfigAsString := strings.TrimRight(strings.TrimLeft(fmt.Sprintf("%v", result["shrinker_config"]), "["), "]")
shrinkerConfigList := strings.Split(shrinkerConfigAsString, " ")
// Massage into newline-separated flag list.
shrinkerConfigFlags := strings.Join(shrinkerConfigList, "\n")
shrinkerConfigFlags := strings.Join(result.ShrinkerConfigFlags, "\n")

// Write the shrinker config to the output file
shrinkerConfigFile, err := os.Create(*outputFileFlag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ set -euxo pipefail
# Create dummy data
dummy_json_file="$(mktemp)"
# Dummy data: shrinker_config is a list of strings, and another unrelated field.
echo "{\"shrinker_config\": [\"a\", \"b\", \"c\"], \"foo\": \"bar\"}" > "$dummy_json_file"
echo "{\"shrinker_config\": [\"-keepclassmembers a b c\", \"foobar\", \"-someflag 1 2 3\"], \"foo\": \"bar\"}" > "$dummy_json_file"

# Dummy output dummy output file
test_output_file="$(mktemp)"
expected_output_file="$(mktemp)"
test_output_file="$(mktemp -p /tmp test.XXXX)"
expected_output_file="$(mktemp -p /tmp golden.XXXX)"
# Expected outcome is a\nb\nc
echo -ne "a\nb\nc" > "$expected_output_file"
echo -ne "-keepclassmembers a b c\nfoobar\n-someflag 1 2 3" > "$expected_output_file"

# Run the binary on the test data
"$BINARY_UNDER_TEST" --input_json "$dummy_json_file" --output_file "$test_output_file"
Expand Down

0 comments on commit 15818b2

Please sign in to comment.