1"use strict";(self.webpackChunkopa_website=self.webpackChunkopa_website||[]).push([[863],{28453:(e,t,s)=>{s.d(t,{R:()=>a,x:()=>o});var n=s(96540);const i={},r=n.createContext(i);function a(e){const t=n.useContext(r);return n.useMemo((function(){return"function"==typeof e?e(t):{...t,...e}}),[t,e])}function o(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:a(e.components),n.createElement(r.Provider,{value:t},e.children)}},81575:(e,t,s)=>{s.r(t),s.d(t,{assets:()=>l,contentTitle:()=>o,default:()=>p,frontMatter:()=>a,metadata:()=>n,toc:()=>c});const n=JSON.parse('{"id":"rules/testing/test-outside-test-package","title":"test-outside-test-package","description":"Summary: Test outside of test package","source":"@site/projects/regal/rules/testing/test-outside-test-package.md","sourceDirName":"rules/testing","slug":"/rules/testing/test-outside-test-package","permalink":"/projects/regal/rules/testing/test-outside-test-package","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{},"sidebar":"autoSidebar","previous":{"title":"print-or-trace-call","permalink":"/projects/regal/rules/testing/print-or-trace-call"},"next":{"title":"todo-test","permalink":"/projects/regal/rules/testing/todo-test"}}');var i=s(74848),r=s(28453);const a={},o="test-outside-test-package",l={},c=[{value:"Rationale",id:"rationale",level:2},{value:"Configuration Options",id:"configuration-options",level:2},{value:"Related Resources",id:"related-resources",level:2}];function d(e){const t={a:"a",code:"code",h1:"h1",h2:"h2",header:"header",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,r.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(t.header,{children:(0,i.jsx)(t.h1,{id:"test-outside-test-package",children:"test-outside-test-package"})}),"\n",(0,i.jsxs)(t.p,{children:[(0,i.jsx)(t.strong,{children:"Summary"}),": Test outside of test package"]}),"\n",(0,i.jsxs)(t.p,{children:[(0,i.jsx)(t.strong,{children:"Category"}),": Testing"]}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.strong,{children:"Avoid"})}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-rego",children:'package policy\n\nallow if {\n "admin" in input.user.roles\n}\n\n# Tests in same package as policy\ntest_allow_if_admin {\n allow with input as {"user": {"roles": ["admin"]}}\n}\n'})}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.strong,{children:"Prefer"})}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-rego",children:'# Tests in separate package with _test suffix\npackage policy_test\n\nimport data.policy\n\ntest_allow_if_admin {\n policy.allow with input as {"user": {"roles": ["admin"]}}\n}\n'})}),"\n",(0,i.jsx)(t.h2,{id:"rationale",children:"Rationale"}),"\n",(0,i.jsxs)(t.p,{children:["While OPA's test runner will evaluate any rules with a ",(0,i.jsx)(t.code,{children:"test_"})," prefix, it is a good practice to clearly separate tests\nfrom production policy. This is easily done by placing tests in a separate package with a ",(0,i.jsx)(t.code,{children:"_test"})," suffix, and correctly\n",(0,i.jsx)(t.a,{href:"https://www.openpolicyagent.org/projects/regal/rules/testing/file-missing-test-suffix",children:"naming"})," the test files."]}),"\n",(0,i.jsx)(t.h2,{id:"configuration-options",children:"Configuration Options"}),"\n",(0,i.jsx)(t.p,{children:"This linter rule provides the following configuration options:"}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-yaml",children:'rules:\n testing:\n test-outside-test-package:\n # one of "error", "warning", "ignore"\n level: error\n'})}),"\n",(0,i.jsx)(t.h2,{id:"related-resources",children:"Related Resources"}),"\n",(0,i.jsxs)(t.ul,{children:["\n",(0,i.jsxs)(t.li,{children:["OPA Docs: ",(0,i.jsx)(t.a,{href:"https://www.openpolicyagent.org/docs/policy-testing/",children:"Policy Testing"})]}),"\n",(0,i.jsxs)(t.li,{children:["GitHub: ",(0,i.jsx)(t.a,{href:"https://github.com/open-policy-agent/regal/blob/main/bundle/regal/rules/testing/test-outside-test-package/test_outside_test_package.rego",children:"Source Code"})]}),"\n"]})]})}function p(e={}){const{wrapper:t}={...(0,r.R)(),...e.components};return t?(0,i.jsx)(t,{...e,children:(0,i.jsx)(d,{...e})}):d(e)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.