Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: allow supressing some package init logics #48

Open
xhd2015 opened this issue Apr 11, 2024 · 1 comment
Open

Feature: allow supressing some package init logics #48

xhd2015 opened this issue Apr 11, 2024 · 1 comment

Comments

@xhd2015
Copy link
Owner

xhd2015 commented Apr 11, 2024

When running unit test, some packages are annoying.Though we are not going to use it while testing, it still stops testing.

Example: vendor/github.com/lucas-clemente/quic-go/internal/handshake/unsafe.go

func init() {
	if !structsEqual(&tls.ConnectionState{}, &qtls.ConnectionState{}) {
		panic("qtls.ConnectionState not compatible with tls.ConnectionState")
	}
	if !structsEqual(&tls.ClientSessionState{}, &qtls.ClientSessionState{}) {
		panic("qtls.ClientSessionState not compatible with tls.ClientSessionState")
	}
}

I want to rewrite it into

func init() {
        if true {
            return
        }
	if !structsEqual(&tls.ConnectionState{}, &qtls.ConnectionState{}) {
		panic("qtls.ConnectionState not compatible with tls.ConnectionState")
	}
	if !structsEqual(&tls.ClientSessionState{}, &qtls.ClientSessionState{}) {
		panic("qtls.ClientSessionState not compatible with tls.ClientSessionState")
	}
}
@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 14, 2024

A solution that does not require changing xgo, using shell to help patch and restore the source file:

#!/usr/bin/env bash

patch_file=vendor/github.com/lucas-clemente/quic-go/internal/handshake/unsafe.go
orig_content=$(cat "$patch_file")

function apply_patch_file {
    cat > "$patch_file" <<EOF
//go:build ignore
// +build ignore

$orig_content
EOF
}

function restore_patch_file {
  echo "$orig_content" > "$patch_file"
}

apply_patch_file
trap restore_patch_file EXIT

xgo test -mod=vendor -v ./...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant