Posts Guard file
Post
Cancel

Guard file

Guardfile to run :

1
2
3
4
pylint
pep8 through flake8
mypy
tests

associated to the file you just saved.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
guard :shell do
  watch(/(?.*\.py)/) do |m|
    file = m[:path]
    exclusion = %w(__pycache__ /migrations/ _BASE_ _REMOTE_ _BACKUP_ _LOCAL_)
    if exclusion.none? { |e| file.include? e }
      command_pylint = "pylint --rcfile=../.pylintrc --load-plugins=pylint_django #{file}"
      puts '', '', "###  Running: #{command_pylint}"
      if not system(command_pylint)
        n "Pylint issues in #{file}", "Pylint", :failed
      end

      command_pep8 = "flake8 #{file}"
      puts '', '', "###  Running: #{command_pep8}"
      if not system(command_pep8)
        n "pep8 issues in #{file}", "pep8", :failed
      end

      command_mypy = "mypy --follow-import=silent #{file}"
      puts '', '', "###  Running: #{command_mypy}"
      if not system(command_mypy)
        n "mypy issues in #{file}", "mypy", :failed
      end
    end
  end
  watch(/(?.*?\/)(?.*\/)?(?.*)\.py/) do |m|
    testModule = ""
    testFile = "#{m[:app]}tests/#{m[:path]}test_#{m[:filename]}"
    if File.file? "#{testFile}.py"
      testModule = testFile.gsub "/", "."
    end
    testFolder = "#{m[:app]}tests/#{m[:path]}#{m[:filename]}"
    if File.directory? testFolder
      testModule = testFolder.gsub "/", "."
    end
    if not testModule.empty?
      command = "python manage.py test --keepdb #{testModule}"
      puts '', '', "###  Running: #{command}"
      if not system(command)
        n "Test errors for #{testModule}", "Django Tests", :failed
      end
    end
  end
  watch(/(?.*\/tests\/(.*\/)?test_.*)\.py/) do |m|
    testModule = m[:filepath].gsub "/", "."
    command = "python manage.py test --keepdb #{testModule}"
    puts '', '', "###  Running: #{command}"
    if not system(command)
      n "Test errors for #{testModule}", "Django Tests", :failed
    end
  end
end

notification  :tmux,
  display_message: true,
  timeout: 2

notification :terminal_notifier if `uname` =~ /Darwin/

On your terminal you have to run

bundle exec guard

This post is licensed under CC BY 4.0 by the author.