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

The testable example "ExamplePrivateHandle" is incorrectly passing #1299

Closed
ameowlia opened this issue Sep 21, 2021 · 4 comments
Closed

The testable example "ExamplePrivateHandle" is incorrectly passing #1299

ameowlia opened this issue Sep 21, 2021 · 4 comments

Comments

@ameowlia
Copy link

👋 Hello,

I am investigating the golang issue: golang/go#48362, where testable examples are causing false positives when the output comment block is not the last comment block example.

I ran a parser against the top golang repos, including this repo and I found this bug with your ExamplePrivateHandle testable example.

If you combine the comment blocks to test as intended (see below) your test fails because the id is not 1234 like the output expects.

This is the code I suspect that you intended:

func ExamplePrivateHandle() {
	dns.PrivateHandle("APAIR", TypeAPAIR, NewAPAIR)
	defer dns.PrivateHandleRemove(TypeAPAIR)

	rr, err := dns.NewRR("miek.nl. APAIR (1.2.3.4    1.2.3.5)")
	if err != nil {
		log.Fatal("could not parse APAIR record: ", err)
	}
	fmt.Println(rr)

	m := new(dns.Msg)
	m.Id = 12345
	m.SetQuestion("miek.nl.", TypeAPAIR)
	m.Answer = append(m.Answer, rr)

	fmt.Println(m)
	
	// Output: miek.nl.	3600	IN	APAIR	1.2.3.4 1.2.3.5
	// ;; opcode: QUERY, status: NOERROR, id: 12345
	// ;; flags: rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
	//
	// ;; QUESTION SECTION:
	// ;miek.nl.	IN	 APAIR
	//
	// ;; ANSWER SECTION:
	// miek.nl.	3600	IN	APAIR	1.2.3.4 1.2.3.5
}

This fails with:

$ go test ./...
--- FAIL: ExamplePrivateHandle (0.00s)
got:
miek.nl.        3600    IN      APAIR   1.2.3.4 1.2.3.5
;; opcode: QUERY, status: NOERROR, id: 49593
;; flags: rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;miek.nl.       IN       APAIR

;; ANSWER SECTION:
miek.nl.        3600    IN      APAIR   1.2.3.4 1.2.3.5
want:
miek.nl.        3600    IN      APAIR   1.2.3.4 1.2.3.5
;; opcode: QUERY, status: NOERROR, id: 12345
;; flags: rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;miek.nl.       IN       APAIR

;; ANSWER SECTION:
miek.nl.        3600    IN      APAIR   1.2.3.4 1.2.3.5
FAIL
FAIL    github.com/miekg/dns    2.974s
ok      github.com/miekg/dns/dnsutil    (cached)
FAIL
@tmthrgd
Copy link
Collaborator

tmthrgd commented Oct 28, 2021

Thanks! I've never been a huge fan of how Go did Example testing and clearly it can be quite britle.

Would you care to open a PR and fix it? If not, let me know and I'll be happy to.

@ameowlia
Copy link
Author

This isn't simple "just move one comment" case. In this case the id changes every time you run this code. I'll let you handle this PR @tmthrgd 😄

@miekg
Copy link
Owner

miekg commented Nov 19, 2021 via email

@miekg
Copy link
Owner

miekg commented Apr 2, 2022

Fixed via #1354

@miekg miekg closed this as completed Apr 2, 2022
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

3 participants