golang.org/x/sys@v0.9.0/windows/mkwinsyscall/mkwinsyscall_test.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"bytes"
     9  	"go/format"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  )
    14  
    15  func TestDLLFilenameEscaping(t *testing.T) {
    16  	tests := []struct {
    17  		name     string
    18  		filename string
    19  	}{
    20  		{"no escaping necessary", "kernel32"},
    21  		{"escape period", "windows.networking"},
    22  		{"escape dash", "api-ms-win-wsl-api-l1-1-0"},
    23  	}
    24  	for _, tt := range tests {
    25  		t.Run(tt.name, func(t *testing.T) {
    26  			// Write a made-up syscall into a temp file for testing.
    27  			const prefix = "package windows\n//sys Example() = "
    28  			const suffix = ".Example"
    29  			name := filepath.Join(t.TempDir(), "syscall.go")
    30  			if err := os.WriteFile(name, []byte(prefix+tt.filename+suffix), 0666); err != nil {
    31  				t.Fatal(err)
    32  			}
    33  
    34  			// Ensure parsing, generating, and formatting run without errors.
    35  			// This is good enough to show that escaping is working.
    36  			src, err := ParseFiles([]string{name})
    37  			if err != nil {
    38  				t.Fatal(err)
    39  			}
    40  			var buf bytes.Buffer
    41  			if err := src.Generate(&buf); err != nil {
    42  				t.Fatal(err)
    43  			}
    44  			if _, err := format.Source(buf.Bytes()); err != nil {
    45  				t.Log(buf.String())
    46  				t.Fatal(err)
    47  			}
    48  		})
    49  	}
    50  }