Skip to content

Commit

Permalink
test: fix faucet test (#4543)
Browse files Browse the repository at this point in the history
* test: fix faucet test

* cl

(cherry picked from commit c60540e)

# Conflicts:
#	ignite/pkg/cosmosfaucet/client_http.go
  • Loading branch information
julienrbrt authored and mergify[bot] committed Feb 26, 2025
1 parent 6e09085 commit b405b51
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 25 deletions.
33 changes: 33 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@
- [#4538](https://github.com/ignite/cli/pull/4538) Create a simple spinner for non-terminal interactions
- [#4540](https://github.com/ignite/cli/pull/4540) Skip logs / gibberish when parsing commands outputs

### Changes

- [#4094](https://github.com/ignite/cli/pull/4094) Scaffolding a multi-index map using `ignite s map foo bar baz --index foobar,foobaz` is no longer supported. Use one index instead of use `collections.IndexedMap`.
- [#4058](https://github.com/ignite/cli/pull/4058) Simplify scaffolded modules by including `ValidateBasic()` logic in message handler.
- [#4058](https://github.com/ignite/cli/pull/4058) Use `address.Codec` instead of `AccAddressFromBech32`.
- [#3993](https://github.com/ignite/cli/pull/3993) Oracle scaffolding was deprecated and has been removed
- [#3962](https://github.com/ignite/cli/pull/3962) Rename all RPC endpoints and autocli commands generated for `map`/`list`/`single` types
- [#3976](https://github.com/ignite/cli/pull/3976) Remove error checks for Cobra command value get calls
- [#4002](https://github.com/ignite/cli/pull/4002) Bump buf build
- [#4008](https://github.com/ignite/cli/pull/4008) Rename `pkg/yaml` to `pkg/xyaml`
- [#4075](https://github.com/ignite/cli/pull/4075) Use `gopkg.in/yaml.v3` instead `gopkg.in/yaml.v2`
- [#4118](https://github.com/ignite/cli/pull/4118) Version scaffolded protos as `v1` to follow SDK structure.
- [#4167](https://github.com/ignite/cli/pull/4167) Scaffold `int64` instead of `int32` when a field type is `int`
- [#4159](https://github.com/ignite/cli/pull/4159) Enable gci linter
- [#4160](https://github.com/ignite/cli/pull/4160) Enable copyloopvar linter
- [#4162](https://github.com/ignite/cli/pull/4162) Enable errcheck linter
- [#4189](https://github.com/ignite/cli/pull/4189) Deprecate `ignite node` for `ignite connect` app
- [#4290](https://github.com/ignite/cli/pull/4290) Remove ignite ics logic from ignite cli (this functionality will be in the `consumer` app)
- [#4295](https://github.com/ignite/cli/pull/4295) Stop scaffolding `pulsar` files
- [#4317](https://github.com/ignite/cli/pull/4317) Remove xchisel dependency
- [#4361](https://github.com/ignite/cli/pull/4361) Remove unused `KeyPrefix` method
- [#4384](https://github.com/ignite/cli/pull/4384) Compare genesis params into chain genesis tests
- [#4463](https://github.com/ignite/cli/pull/4463) Run `chain simulation` with any simulation test case
- [#4533](https://github.com/ignite/cli/pull/4533) Promote GitHub codespace instead of Gitpod

### Fixes

- [#4000](https://github.com/ignite/cli/pull/4000) Run all dry runners before the wet run in the `xgenny` pkg
- [#4091](https://github.com/ignite/cli/pull/4091) Fix race conditions in the plugin logic
- [#4128](https://github.com/ignite/cli/pull/4128) Check for duplicate proto fields in config
- [#4402](https://github.com/ignite/cli/pull/4402) Fix gentx parser into the cosmosutil package
- [#4540](https://github.com/ignite/cli/pull/4540), [#4543](https://github.com/ignite/cli/pull/4543) Skip logs / gibberish when parsing commands outputs

## [`v28.8.0`](https://github.com/ignite/cli/releases/tag/v28.8.0)

### Features
Expand Down
114 changes: 90 additions & 24 deletions ignite/pkg/chaincmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,47 +143,113 @@ type buffer struct {

// JSONEnsuredBytes ensures that encoding format for returned bytes is always
// JSON even if the written data is originally encoded in YAML.
// This method is purposely verbose to trim gibberish output.
func (b *buffer) JSONEnsuredBytes() ([]byte, error) {
bz := b.Bytes()
content := strings.TrimSpace(string(bz))

// check for valid json
// Early detection - check first non-whitespace character
if len(content) > 0 {
firstChar := content[0]

// Quick check for JSON format (starts with { or [)
if firstChar == '{' || firstChar == '[' {
// Attempt to validate and extract clean JSON
return cleanAndValidateJSON(bz)
}

// Quick check for YAML format (common indicators)
if firstChar == '-' || strings.HasPrefix(content, "---") ||
strings.Contains(content, ":\n") || strings.Contains(content, ": ") {
// Likely YAML, convert to JSON directly
var out any
if err := yaml.Unmarshal(bz, &out); err == nil {
return yaml.YAMLToJSON(bz)
}
}
}

// If format wasn't immediately obvious, try the more thorough approach
return fallbackFormatDetection(bz)
}

// cleanAndValidateJSON attempts to extract valid JSON from potentially messy output
func cleanAndValidateJSON(bz []byte) ([]byte, error) {
// Find the first JSON opening character
startIndex := strings.IndexAny(string(bz), "{[")
if startIndex >= 0 {
// check if we need to find the matching closing bracket
opening := bz[startIndex]
var closing byte
if opening == '{' {
closing = '}'
} else {
closing = ']'
if startIndex < 0 {
return bz, nil // No JSON structure found
}

// Determine matching closing character
opening := bz[startIndex]
var closing byte
if opening == '{' {
closing = '}'
} else {
closing = ']'
}

endIndex := findMatchingCloseBracket(bz[startIndex:], opening, closing)
if endIndex < 0 {
// no proper closing found, try last instance
endIndex = bytes.LastIndexByte(bz, closing)
if endIndex <= startIndex {
return bz[startIndex:], nil // Return from start to end if no closing found
}
} else {
endIndex += startIndex
}

// look for the last matching closing bracket
endIndex := bytes.LastIndexByte(bz, closing)
if endIndex > startIndex {
// extract what appears to be valid JSON
bz = bz[startIndex : endIndex+1]
// validate JSON
jsonData := bz[startIndex : endIndex+1]
var jsonTest any
if err := json.Unmarshal(jsonData, &jsonTest); err == nil {
return jsonData, nil
}

// if validation failed, return from start to end
return bz[startIndex:], nil
}

// verify it's actually valid JSON
var jsonTest any
if err := json.Unmarshal(bz, &jsonTest); err == nil {
return bz, nil
// findMatchingCloseBracket finds the index of the matching closing bracket
// accounting for nested structures
func findMatchingCloseBracket(data []byte, openChar, closeChar byte) int {
depth := 0
for i, b := range data {
if b == openChar {
depth++
} else if b == closeChar {
depth--
if depth == 0 {
return i // Found matching closing bracket
}
}
}
return -1 // No matching bracket found
}

// fallbackFormatDetection tries different approaches to detect and convert format
func fallbackFormatDetection(bz []byte) ([]byte, error) {
// first try to find and extract JSON
startIndex := strings.IndexAny(string(bz), "{[")
if startIndex >= 0 {
result, err := cleanAndValidateJSON(bz)
if err == nil {
return result, nil
}

// if extraction failed but we found a start, return from there
return bz[startIndex:], nil
}

// fallback to yaml parsing
var out any
if err := yaml.Unmarshal(bz, &out); err == nil {
return yaml.YAMLToJSON(bz)
}

// if neither JSON nor YAML parsing succeeded, return the original bytes
// starting from the first opening brace if found, or the entire buffer
if startIndex >= 0 {
return bz[startIndex:], nil
}

// nothing worked, return original
return bz, nil
}

Expand Down
13 changes: 12 additions & 1 deletion ignite/pkg/cosmosfaucet/client_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"

"github.com/ignite/cli/v28/ignite/pkg/errors"
)

// ErrTransferRequest is an error that occurs when a transfer request fails.
type ErrTransferRequest struct {
Body string
StatusCode int
}

Expand Down Expand Up @@ -48,12 +50,21 @@ func (c HTTPClient) Transfer(ctx context.Context, req TransferRequest) (Transfer
defer hres.Body.Close()

if hres.StatusCode != http.StatusOK {
return TransferResponse{}, ErrTransferRequest{hres.StatusCode}
bodyBytes, _ := io.ReadAll(hres.Body)
return TransferResponse{}, ErrTransferRequest{Body: string(bodyBytes), StatusCode: hres.StatusCode}
}

var res TransferResponse
<<<<<<< HEAD

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test account on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test cosmosgen on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test chain on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test relayer on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test simulation on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test other_components on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test doctor on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test ibc on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test tx on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test list on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test single on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test node on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test faucet on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test app on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test map on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test plugin on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test chain on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test ibc on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test app on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test other_components on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test plugin on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test faucet on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test map on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test account on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test node on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test doctor on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test cosmosgen on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test network on ubuntu-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test single on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test list on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test relayer on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test simulation on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test network on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test tx on macos-latest

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

syntax error: unexpected <<, expected }

Check failure on line 58 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

syntax error: unexpected <<, expected }
err = json.NewDecoder(hres.Body).Decode(&res)
return res, err
=======
if err = json.NewDecoder(hres.Body).Decode(&res); err != nil {
return TransferResponse{}, err
}

return res, nil
>>>>>>> c60540e0 (test: fix faucet test (#4543))

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test account on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test cosmosgen on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test chain on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test relayer on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test simulation on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test other_components on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test doctor on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test ibc on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test tx on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test list on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test single on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test node on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test faucet on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test app on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test map on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test plugin on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test chain on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test ibc on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test app on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test other_components on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test plugin on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test faucet on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test map on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test account on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test node on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test doctor on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test cosmosgen on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test network on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test single on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test list on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test relayer on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test simulation on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test network on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test tx on macos-latest

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

invalid character U+0023 '#'

Check failure on line 67 in ignite/pkg/cosmosfaucet/client_http.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

invalid character U+0023 '#'
}

// FaucetInfo fetch the faucet info for clients to determine if this is a real faucet and
Expand Down

0 comments on commit b405b51

Please sign in to comment.