1
0

processtest_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2021 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build !android && !ios && !js
  15. package processtest_test
  16. import (
  17. "bytes"
  18. "context"
  19. "os"
  20. "os/exec"
  21. "path/filepath"
  22. "runtime"
  23. "strings"
  24. "sync"
  25. "testing"
  26. "time"
  27. )
  28. func isWSL() (bool, error) {
  29. if runtime.GOOS != "windows" {
  30. return false, nil
  31. }
  32. abs, err := filepath.Abs(".")
  33. if err != nil {
  34. return false, err
  35. }
  36. return strings.HasPrefix(abs, `\\wsl$\`), nil
  37. }
  38. func TestPrograms(t *testing.T) {
  39. wsl, err := isWSL()
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. if wsl {
  44. t.Skip("WSL doesn't support LockFileEx (#1864)")
  45. }
  46. dir := "testdata"
  47. ents, err := os.ReadDir(dir)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. tmpdir := t.TempDir()
  52. // Run sub-tests one by one, not in parallel (#2571).
  53. var m sync.Mutex
  54. for _, e := range ents {
  55. if e.IsDir() {
  56. continue
  57. }
  58. n := e.Name()
  59. if !strings.HasSuffix(n, ".go") {
  60. continue
  61. }
  62. t.Run(n, func(t *testing.T) {
  63. m.Lock()
  64. defer m.Unlock()
  65. bin := filepath.Join(tmpdir, n)
  66. if out, err := exec.Command("go", "build", "-o", bin, filepath.Join(dir, n)).CombinedOutput(); err != nil {
  67. t.Fatalf("%v\n%s", err, string(out))
  68. }
  69. ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
  70. defer cancel()
  71. cmd := exec.CommandContext(ctx, bin)
  72. stderr := &bytes.Buffer{}
  73. cmd.Stderr = stderr
  74. if err := cmd.Run(); err != nil {
  75. t.Errorf("%v\n%s", err, stderr)
  76. }
  77. })
  78. }
  79. }