/src/go-src-converted/testing/internal/testdeps/deps.cs

https://github.com/GridProtectionAlliance/go2cs · C# · 171 lines · 118 code · 32 blank · 21 comment · 12 complexity · 643ad5652217b0706e7d63d07f65d979 MD5 · raw file

  1. // Copyright 2016 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. // Package testdeps provides access to dependencies needed by test execution.
  5. //
  6. // This package is imported by the generated main package, which passes
  7. // TestDeps into testing.Main. This allows tests to use packages at run time
  8. // without making those packages direct dependencies of package testing.
  9. // Direct dependencies of package testing are harder to write tests for.
  10. // package testdeps -- go2cs converted at 2020 October 09 05:47:47 UTC
  11. // import "testing/internal/testdeps" ==> using testdeps = go.testing.@internal.testdeps_package
  12. // Original source: C:\Go\src\testing\internal\testdeps\deps.go
  13. using bufio = go.bufio_package;
  14. using testlog = go.@internal.testlog_package;
  15. using io = go.io_package;
  16. using regexp = go.regexp_package;
  17. using pprof = go.runtime.pprof_package;
  18. using strings = go.strings_package;
  19. using sync = go.sync_package;
  20. using static go.builtin;
  21. namespace go {
  22. namespace testing {
  23. namespace @internal
  24. {
  25. public static partial class testdeps_package
  26. {
  27. // TestDeps is an implementation of the testing.testDeps interface,
  28. // suitable for passing to testing.MainStart.
  29. public partial struct TestDeps
  30. {
  31. }
  32. private static @string matchPat = default;
  33. private static ptr<regexp.Regexp> matchRe;
  34. public static (bool, error) MatchString(this TestDeps _p0, @string pat, @string str)
  35. {
  36. bool result = default;
  37. error err = default!;
  38. if (matchRe == null || matchPat != pat)
  39. {
  40. matchPat = pat;
  41. matchRe, err = regexp.Compile(matchPat);
  42. if (err != null)
  43. {
  44. return ;
  45. }
  46. }
  47. return (matchRe.MatchString(str), error.As(null!)!);
  48. }
  49. public static error StartCPUProfile(this TestDeps _p0, io.Writer w)
  50. {
  51. return error.As(pprof.StartCPUProfile(w))!;
  52. }
  53. public static void StopCPUProfile(this TestDeps _p0)
  54. {
  55. pprof.StopCPUProfile();
  56. }
  57. public static error WriteProfileTo(this TestDeps _p0, @string name, io.Writer w, long debug)
  58. {
  59. return error.As(pprof.Lookup(name).WriteTo(w, debug))!;
  60. }
  61. // ImportPath is the import path of the testing binary, set by the generated main function.
  62. public static @string ImportPath = default;
  63. public static @string ImportPath(this TestDeps _p0)
  64. {
  65. return ImportPath;
  66. }
  67. // testLog implements testlog.Interface, logging actions by package os.
  68. private partial struct testLog
  69. {
  70. public sync.Mutex mu;
  71. public ptr<bufio.Writer> w;
  72. public bool set;
  73. }
  74. private static void Getenv(this ptr<testLog> _addr_l, @string key)
  75. {
  76. ref testLog l = ref _addr_l.val;
  77. l.add("getenv", key);
  78. }
  79. private static void Open(this ptr<testLog> _addr_l, @string name)
  80. {
  81. ref testLog l = ref _addr_l.val;
  82. l.add("open", name);
  83. }
  84. private static void Stat(this ptr<testLog> _addr_l, @string name)
  85. {
  86. ref testLog l = ref _addr_l.val;
  87. l.add("stat", name);
  88. }
  89. private static void Chdir(this ptr<testLog> _addr_l, @string name)
  90. {
  91. ref testLog l = ref _addr_l.val;
  92. l.add("chdir", name);
  93. }
  94. // add adds the (op, name) pair to the test log.
  95. private static void add(this ptr<testLog> _addr_l, @string op, @string name) => func((defer, _, __) =>
  96. {
  97. ref testLog l = ref _addr_l.val;
  98. if (strings.Contains(name, "\n") || name == "")
  99. {
  100. return ;
  101. }
  102. l.mu.Lock();
  103. defer(l.mu.Unlock());
  104. if (l.w == null)
  105. {
  106. return ;
  107. }
  108. l.w.WriteString(op);
  109. l.w.WriteByte(' ');
  110. l.w.WriteString(name);
  111. l.w.WriteByte('\n');
  112. });
  113. private static testLog log = default;
  114. public static void StartTestLog(this TestDeps _p0, io.Writer w)
  115. {
  116. log.mu.Lock();
  117. log.w = bufio.NewWriter(w);
  118. if (!log.set)
  119. {
  120. // Tests that define TestMain and then run m.Run multiple times
  121. // will call StartTestLog/StopTestLog multiple times.
  122. // Checking log.set avoids calling testlog.SetLogger multiple times
  123. // (which will panic) and also avoids writing the header multiple times.
  124. log.set = true;
  125. testlog.SetLogger(_addr_log);
  126. log.w.WriteString("# test log\n"); // known to cmd/go/internal/test/test.go
  127. }
  128. log.mu.Unlock();
  129. }
  130. public static error StopTestLog(this TestDeps _p0) => func((defer, _, __) =>
  131. {
  132. log.mu.Lock();
  133. defer(log.mu.Unlock());
  134. var err = log.w.Flush();
  135. log.w = null;
  136. return error.As(err)!;
  137. });
  138. }
  139. }}}