forbidden_words_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (C) 2021 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package meta
  7. import (
  8. "bytes"
  9. "os"
  10. "path/filepath"
  11. "strings"
  12. "testing"
  13. )
  14. // Checks for forbidden words in all .go files
  15. func TestForbiddenWords(t *testing.T) {
  16. checkDirs := []string{"../cmd", "../lib", "../test", "../script"}
  17. forbiddenWords := []string{
  18. `"io/ioutil"`, // deprecated and should not be imported
  19. }
  20. for _, dir := range checkDirs {
  21. err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  22. if err != nil {
  23. return err
  24. }
  25. if path == ".git" {
  26. return filepath.SkipDir
  27. }
  28. if filepath.Ext(path) != ".go" || strings.HasSuffix(path, ".pb.go") {
  29. return nil
  30. }
  31. bs, err := os.ReadFile(path)
  32. if err != nil {
  33. return err
  34. }
  35. for _, word := range forbiddenWords {
  36. if bytes.Contains(bs, []byte(word)) {
  37. t.Errorf("%s: forbidden word %q", path, word)
  38. }
  39. }
  40. return nil
  41. })
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. }
  46. }