ae_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2016 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package main
  5. func init() {
  6. addTestCases(aeTests, nil)
  7. }
  8. var aeTests = []testCase{
  9. // Collection of fixes:
  10. // - imports
  11. // - appengine.Timeout -> context.WithTimeout
  12. // - add ctx arg to appengine.Datacenter
  13. // - logging API
  14. {
  15. Name: "ae.0",
  16. In: `package foo
  17. import (
  18. "net/http"
  19. "time"
  20. "appengine"
  21. "appengine/datastore"
  22. )
  23. func f(w http.ResponseWriter, r *http.Request) {
  24. c := appengine.NewContext(r)
  25. c = appengine.Timeout(c, 5*time.Second)
  26. err := datastore.ErrNoSuchEntity
  27. c.Errorf("Something interesting happened: %v", err)
  28. _ = appengine.Datacenter()
  29. }
  30. `,
  31. Out: `package foo
  32. import (
  33. "net/http"
  34. "time"
  35. "golang.org/x/net/context"
  36. "google.golang.org/appengine"
  37. "google.golang.org/appengine/datastore"
  38. "google.golang.org/appengine/log"
  39. )
  40. func f(w http.ResponseWriter, r *http.Request) {
  41. c := appengine.NewContext(r)
  42. c, _ = context.WithTimeout(c, 5*time.Second)
  43. err := datastore.ErrNoSuchEntity
  44. log.Errorf(c, "Something interesting happened: %v", err)
  45. _ = appengine.Datacenter(c)
  46. }
  47. `,
  48. },
  49. // Updating a function that takes an appengine.Context arg.
  50. {
  51. Name: "ae.1",
  52. In: `package foo
  53. import (
  54. "appengine"
  55. )
  56. func LogSomething(c2 appengine.Context) {
  57. c2.Warningf("Stand back! I'm going to try science!")
  58. }
  59. `,
  60. Out: `package foo
  61. import (
  62. "golang.org/x/net/context"
  63. "google.golang.org/appengine/log"
  64. )
  65. func LogSomething(c2 context.Context) {
  66. log.Warningf(c2, "Stand back! I'm going to try science!")
  67. }
  68. `,
  69. },
  70. // Less widely used API changes:
  71. // - drop maxTasks arg to taskqueue.QueueStats
  72. {
  73. Name: "ae.2",
  74. In: `package foo
  75. import (
  76. "appengine"
  77. "appengine/taskqueue"
  78. )
  79. func f(ctx appengine.Context) {
  80. stats, err := taskqueue.QueueStats(ctx, []string{"one", "two"}, 0)
  81. }
  82. `,
  83. Out: `package foo
  84. import (
  85. "golang.org/x/net/context"
  86. "google.golang.org/appengine/taskqueue"
  87. )
  88. func f(ctx context.Context) {
  89. stats, err := taskqueue.QueueStats(ctx, []string{"one", "two"})
  90. }
  91. `,
  92. },
  93. // Check that the main "appengine" import will not be dropped
  94. // if an appengine.Context -> context.Context change happens
  95. // but the appengine package is still referenced.
  96. {
  97. Name: "ae.3",
  98. In: `package foo
  99. import (
  100. "appengine"
  101. "io"
  102. )
  103. func f(ctx appengine.Context, w io.Writer) {
  104. _ = appengine.IsDevAppServer()
  105. }
  106. `,
  107. Out: `package foo
  108. import (
  109. "golang.org/x/net/context"
  110. "google.golang.org/appengine"
  111. "io"
  112. )
  113. func f(ctx context.Context, w io.Writer) {
  114. _ = appengine.IsDevAppServer()
  115. }
  116. `,
  117. },
  118. }