contributing.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. Contributing
  2. ============
  3. Contributing happens via "Pull requests" (PR) on github. Every PR needs to be
  4. reviewed before it can be merged and the Continuous Integration should be green.
  5. The PR has to be approved (and is often merged too) by one "code owner", either
  6. by the code owner who is responsible for the subsystem the PR belongs to or by
  7. two core developers or by Araq.
  8. See `codeowners <codeowners.html>`_ for more details.
  9. Writing tests
  10. =============
  11. There are 3 types of tests:
  12. 1. ``runnableExamples`` documentation comment tests, ran by ``nim doc mymod.nim``
  13. These end up in documentation and ensure documentation stays in sync with code.
  14. 2. tests in ``when isMainModule:`` block, ran by ``nim c mymod.nim``
  15. ``nimble test`` also typially runs these in external nimble packages.
  16. 3. testament tests, eg: tests/stdlib/tos.nim (only used for Nim repo).
  17. Not all the tests follow the convention here, feel free to change the ones
  18. that don't. Always leave the code cleaner than you found it.
  19. Stdlib
  20. ------
  21. If you change the stdlib (anything under ``lib/``, eg ``lib/pure/os.nim``),
  22. put a test in the file you changed. Add the tests under a ``when isMainModule:``
  23. condition so they only get executed when the tester is building the
  24. file. Each test should be in a separate ``block:`` statement, such that
  25. each has its own scope. Use boolean conditions and ``doAssert`` for the
  26. testing by itself, don't rely on echo statements or similar.
  27. Sample test:
  28. .. code-block:: nim
  29. when isMainModule:
  30. block: # newSeqWith tests
  31. var seq2D = newSeqWith(4, newSeq[bool](2))
  32. seq2D[0][0] = true
  33. seq2D[1][0] = true
  34. seq2D[0][1] = true
  35. doAssert seq2D == @[@[true, true], @[true, false],
  36. @[false, false], @[false, false]]
  37. # doAssert with `not` can be done as follows:
  38. doAssert: not 1 == 2
  39. Newer tests tend to be run via ``testament`` rather than via ``when isMainModule:``,
  40. eg ``tests/stdlib/tos.nim``; this allows additional features such as custom
  41. compiler flags; for more details see below.
  42. Compiler
  43. --------
  44. The tests for the compiler use a testing tool called ``testament``. They are all
  45. located in ``tests/`` (eg: ``tests/destructor/tdestructor3.nim``).
  46. Each test has its own file. All test files are prefixed with ``t``. If you want
  47. to create a file for import into another test only, use the prefix ``m``.
  48. At the beginning of every test is the expected behavior of the test.
  49. Possible keys are:
  50. - output: The expected output, most likely via ``echo``
  51. - exitcode: Exit code of the test (via ``exit(number)``)
  52. - errormsg: The expected error message
  53. - file: The file the errormsg was produced at
  54. - line: The line the errormsg was produced at
  55. For a full spec, see here: ``testament/specs.nim``
  56. An example for a test:
  57. .. code-block:: nim
  58. discard """
  59. errormsg: "type mismatch: got (PTest)"
  60. """
  61. type
  62. PTest = ref object
  63. proc test(x: PTest, y: int) = nil
  64. var buf: PTest
  65. buf.test()
  66. Running tests
  67. =============
  68. You can run the tests with
  69. ::
  70. ./koch tests
  71. which will run a good subset of tests. Some tests may fail. If you
  72. only want to see the output of failing tests, go for
  73. ::
  74. ./koch tests --failing all
  75. You can also run only a single category of tests. A category is a subdirectory
  76. in the ``tests`` directory. There are a couple of special categories; for a
  77. list of these, see ``testament/categories.nim``, at the bottom.
  78. ::
  79. ./koch tests c lib
  80. For reproducible tests (to reproduce an environment more similar to the one
  81. run by Continuous Integration on travis/appveyor), you may want to disable your
  82. local configuration (eg in ``~/.config/nim/nim.cfg``) which may affect some
  83. tests; this can also be achieved by using
  84. ``export XDG_CONFIG_HOME=pathtoAlternateConfig`` before running ``./koch``
  85. commands.
  86. Comparing tests
  87. ===============
  88. Because some tests fail in the current ``devel`` branch, not every failure
  89. after your change is necessarily caused by your changes. Some tests are
  90. flaky and will fail on occasion; these are typically bugs that should be fixed.
  91. Test failures can be grepped using ``Failure:``.
  92. The tester can compare two test runs. First, you need to create the
  93. reference test. You'll also need to the commit id, because that's what
  94. the tester needs to know in order to compare the two.
  95. ::
  96. git checkout devel
  97. DEVEL_COMMIT=$(git rev-parse HEAD)
  98. ./koch tests
  99. Then switch over to your changes and run the tester again.
  100. ::
  101. git checkout your-changes
  102. ./koch tests
  103. Then you can ask the tester to create a ``testresults.html`` which will
  104. tell you if any new tests passed/failed.
  105. ::
  106. ./koch tests --print html $DEVEL_COMMIT
  107. Deprecation
  108. ===========
  109. Backward compatibility is important, so instead of a rename you need to deprecate
  110. the old name and introduce a new name:
  111. .. code-block:: nim
  112. # for routines (proc/template/macro/iterator) and types:
  113. proc oldProc() {.deprecated: "use `newImpl: string -> int` instead".} = ...
  114. # for (const/var/let) the msg is not yet supported:
  115. const Foo {.deprecated.} = 1
  116. # for enum types ``deprecated`` is not yet supported.
  117. See also `Deprecated <https://nim-lang.org/docs/manual.html#pragmas-deprecated-pragma>`_
  118. pragma in the manual.
  119. Documentation
  120. =============
  121. When contributing new procs, be sure to add documentation, especially if
  122. the proc is public. Documentation begins on the line
  123. following the ``proc`` definition, and is prefixed by ``##`` on each line.
  124. Runnable code examples are also encouraged, to show typical behavior with a few
  125. test cases (typically 1 to 3 ``doAssert`` statements, depending on complexity).
  126. These ``runnableExamples`` are automatically run by ``nim doc mymodule.nim``
  127. as well as ``testament`` and guarantee they stay in sync.
  128. .. code-block:: nim
  129. proc addBar*(a: string): string =
  130. ## Adds "Bar" to ``a``.
  131. runnableExamples:
  132. doAssert "baz".addBar == "bazBar"
  133. result = a & "Bar"
  134. See `parentDir <https://nim-lang.github.io/Nim/os.html#parentDir%2Cstring>`_
  135. example.
  136. The RestructuredText Nim uses has a special syntax for including code snippets
  137. embedded in documentation; these are not run by ``nim doc`` and therefore are
  138. not guaranteed to stay in sync, so ``runnableExamples`` is usually preferred:
  139. .. code-block:: nim
  140. proc someproc*(): string =
  141. ## Return "something"
  142. ##
  143. ## .. code-block:: nim
  144. ##
  145. ## echo someproc() # "something"
  146. result = "something" # single-hash comments do not produce documentation
  147. The ``.. code-block:: nim`` followed by a newline and an indentation instructs the
  148. ``nim doc`` command to produce syntax-highlighted example code with the
  149. documentation.
  150. When forward declaration is used, the documentation should be included with the
  151. first appearance of the proc.
  152. .. code-block:: nim
  153. proc hello*(): string
  154. ## Put documentation here
  155. proc nothing() = discard
  156. proc hello*(): string =
  157. ## ignore this
  158. echo "hello"
  159. The preferred documentation style is to begin with a capital letter and use
  160. the imperative (command) form. That is, between:
  161. .. code-block:: nim
  162. proc hello*(): string =
  163. # Return "hello"
  164. result = "hello"
  165. or
  166. .. code-block:: nim
  167. proc hello*(): string =
  168. # says hello
  169. result = "hello"
  170. the first is preferred.
  171. Best practices
  172. =============
  173. Note: these are general guidelines, not hard rules; there are always exceptions.
  174. Code reviews can just point to a specific section here to save time and
  175. propagate best practices.
  176. .. _noimplicitbool:
  177. Take advantage of no implicit bool conversion
  178. .. code-block:: nim
  179. doAssert isValid() == true
  180. doAssert isValid() # preferred
  181. .. _design_for_mcs:
  182. Design with method call syntax chaining in mind
  183. .. code-block:: nim
  184. proc foo(cond: bool, lines: seq[string]) # bad
  185. proc foo(lines: seq[string], cond: bool) # preferred
  186. # can be called as: `getLines().foo(false)`
  187. .. _avoid_quit:
  188. Use exceptions (including assert / doAssert) instead of ``quit``
  189. rationale: https://forum.nim-lang.org/t/4089
  190. .. code-block:: nim
  191. quit() # bad in almost all cases
  192. doAssert() # preferred
  193. .. _tests_use_doAssert:
  194. Use ``doAssert`` (or ``require``, etc), not ``assert`` in all tests.
  195. .. code-block:: nim
  196. runnableExamples: assert foo() # bad
  197. runnableExamples: doAssert foo() # preferred
  198. .. _delegate_printing:
  199. Delegate printing to caller: return ``string`` instead of calling ``echo``
  200. rationale: it's more flexible (eg allows caller to call custom printing,
  201. including prepending location info, writing to log files, etc).
  202. .. code-block:: nim
  203. proc foo() = echo "bar" # bad
  204. proc foo(): string = "bar" # preferred (usually)
  205. .. _use_Option:
  206. [Ongoing debate] Consider using Option instead of return bool + var argument,
  207. unless stack allocation is needed (eg for efficiency).
  208. .. code-block:: nim
  209. proc foo(a: var Bar): bool
  210. proc foo(): Option[Bar]
  211. .. _use_doAssert_not_echo:
  212. Tests (including in testament) should always prefer assertions over ``echo``,
  213. except when that's not possible. It's more precise, easier for readers and
  214. maintaners to where expected values refer to. See for example
  215. https://github.com/nim-lang/Nim/pull/9335 and https://forum.nim-lang.org/t/4089
  216. .. code-block:: nim
  217. echo foo() # adds a line in testament `discard` block.
  218. doAssert foo() == [1, 2] # preferred, except when not possible to do so.
  219. The Git stuff
  220. =============
  221. General commit rules
  222. --------------------
  223. 1. Bugfixes that should be backported to the latest stable release should
  224. contain the string ``[backport]`` in the commit message! There will be an
  225. outmated process relying on these. However, bugfixes also have the inherent
  226. risk of causing regressions which are worse for a "stable, bugfixes-only"
  227. branch, so in doubt, leave out the ``[backport]``. Standard library bugfixes
  228. are less critical than compiler bugfixes.
  229. 2. All changes introduced by the commit (diff lines) must be related to the
  230. subject of the commit.
  231. If you change something unrelated to the subject parts of the file, because
  232. your editor reformatted automatically the code or whatever different reason,
  233. this should be excluded from the commit.
  234. *Tip:* Never commit everything as is using ``git commit -a``, but review
  235. carefully your changes with ``git add -p``.
  236. 3. Changes should not introduce any trailing whitespace.
  237. Always check your changes for whitespace errors using ``git diff --check``
  238. or add following ``pre-commit`` hook:
  239. .. code-block:: sh
  240. #!/bin/sh
  241. git diff --check --cached || exit $?
  242. 4. Describe your commit and use your common sense.
  243. Example Commit messages: ``Fixes #123; refs #124``
  244. indicates that issue ``#123`` is completely fixed (github may automatically
  245. close it when the PR is committed), wheres issue ``#124`` is referenced
  246. (eg: partially fixed) and won't close the issue when committed.
  247. 5. Commits should be always be rebased against devel (so a fast forward
  248. merge can happen)
  249. eg: use ``git pull --rebase origin devel``. This is to avoid messing up
  250. git history, see `#8664 <https://github.com/nim-lang/Nim/issues/8664>`_ .
  251. Exceptions should be very rare: when rebase gives too many conflicts, simply
  252. squash all commits using the script shown in
  253. https://github.com/nim-lang/Nim/pull/9356
  254. 6. Do not mix pure formatting changes (eg whitespace changes, nimpretty) or
  255. automated changes (eg nimfix) with other code changes: these should be in
  256. separate commits (and the merge on github should not squash these into 1).
  257. Continuous Integration (CI)
  258. ---------------------------
  259. 1. Continuous Integration is by default run on every push in a PR; this clogs
  260. the CI pipeline and affects other PR's; if you don't need it (eg for WIP or
  261. documentation only changes), add ``[ci skip]`` to your commit message title.
  262. This convention is supported by `Appveyor <https://www.appveyor.com/docs/how-to/filtering-commits/#skip-directive-in-commit-message>`_
  263. and `Travis <https://docs.travis-ci.com/user/customizing-the-build/#skipping-a-build>`_
  264. 2. Consider enabling CI (travis and appveyor) in your own Nim fork, and
  265. waiting for CI to be green in that fork (fixing bugs as needed) before
  266. opening your PR in original Nim repo, so as to reduce CI congestion. Same
  267. applies for updates on a PR: you can test commits on a separate private
  268. branch before updating the main PR.
  269. Code reviews
  270. ------------
  271. 1. Whenever possible, use github's new 'Suggested change' in code reviews, which
  272. saves time explaining the change or applying it; see also
  273. https://forum.nim-lang.org/t/4317
  274. .. include:: docstyle.rst