
    %j'              	       (   d Z ddlZddlmc mZ ddlZddlZddl	Z	ddl
Z
ddlZddlZdZej                  j                  dej                   j#                  ed            ZdZddeded	ed
efdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"y)u   
회귀 테스트: task-2729+17 GOAL-GATE placeholder hardening
검증 대상: goal_assertion_eval bash 함수 (finish-task.sh 내)
작성자: 벨레스 (개발6팀 테스터)
    Nz0/home/jay/workspace/.worktrees/task-2729+17-dev6FINISH_TASK_SCRIPTzscripts/finish-task.sh   cmdallowedtmoreturnc                 p   dt        j                  t               dt        j                  |        dt        j                  |       d| }t        j                  dd|gddt
        t              }|j                  j                         r+|j                  j                         j                         d   S d	S )
uU   goal_assertion_eval 을 라이브러리 모드로 호출하고 stdout 첫 줄 반환.FINISH_TASK_LIB_ONLY=1 source z; goal_assertion_eval  bash-cTcapture_outputtextcwdtimeoutr    )
shlexquoteSCRIPT
subprocessrunWORKTREESUBPROCESS_TIMEOUTstdoutstrip
splitlines)r   r   r   bash_snippetresults        q/home/jay/workspace/.worktrees/task-2729+17-dev6/tests/regression/test_goal_gate_placeholder_hardening_2729p17.py
_call_goalr!      s     )V)<(= >${{3/0%++g2F1Gq	O  ^^	|$"F 5;MM4G4G4I6== ++-a0QrQ    c                     d} d}t        | |d      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |      rt        j                  |      ndt        j                  |      d	z  }t        j                  d
|      dz   d|iz  }t        t        j                  |            dx}}y)uQ   python3 $QC_SCRIPT --gate --task-id ... → SKIP_PLACEHOLDER (실행 전 차단).z'python3 $QC_SCRIPT --gate --task-id ...python3   r   SKIP_PLACEHOLDER==z%(py0)s == %(py3)sr   py0py3uT   미확장 변수+리터럴 ... 포함 명령은 SKIP_PLACEHOLDER 이어야 함. got=
>assert %(py5)spy5N
r!   
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_saferepr_format_assertmsgAssertionError_format_explanationr   r   r   @py_assert2@py_assert1@py_format4@py_format6s          r    -test_case1_mixed_placeholder_var_and_ellipsisr?   )   s    
3CGW!,F' 6''  6'              (    __e^hi    r"   c                     d} d}t        | |d      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |      rt        j                  |      ndt        j                  |      d	z  }t        j                  d
|      dz   d|iz  }t        t        j                  |            dx}}y)u4   cat ... → SKIP_PLACEHOLDER (리터럴 ... 탐지).zcat ...catr%   r&   r'   r(   r*   r   r+   uC   리터럴 ... 포함 명령은 SKIP_PLACEHOLDER 이어야 함. got=r.   r/   Nr0   r:   s          r    test_case2_ellipsis_placeholderrB   4   s    
CGW!,F' 6''  6'              (    NfZX    r"   c                     d} d}t        | |d      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |      rt        j                  |      ndt        j                  |      d	z  }t        j                  d
d d|      dz   d|iz  }t        t        j                  |            dx}}y)uE   python3 ${TASK_ID}.py → SKIP_PLACEHOLDER (미확장 ${VAR} 탐지).zpython3 ${TASK_ID}.pyr$   r%   r&   r'   r(   r*   r   r+   u   미확장 $z	{TASK_ID}u6    포함 명령은 SKIP_PLACEHOLDER 이어야 함. got=r.   r/   Nr0   r:   s          r    %test_case3_unexpanded_var_placeholderrD   ?   s    
!CGW!,F' 6''  6'              (    k]"XY_Xbc    r"   c                     d} d}t        | |d      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |      rt        j                  |      ndt        j                  |      d	z  }t        j                  d
|      dz   d|iz  }t        t        j                  |            dx}}y)uL   grep -q $PATTERN /etc/hostname → SKIP_PLACEHOLDER (미확장 $VAR 탐지).zgrep -q $PATTERN /etc/hostnamegrepr%   r&   r'   r(   r*   r   r+   uD   미확장 $VAR 포함 명령은 SKIP_PLACEHOLDER 이어야 함. got=r.   r/   Nr0   r:   s          r    !test_case3b_unexpanded_dollar_varrG   I   s    
*CGW!,F' 6''  6'              (    OvjY    r"   c                     d} d}t        | |d      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |      rt        j                  |      ndt        j                  |      d	z  }t        j                  d
|      dz   d|iz  }t        t        j                  |            dx}}y)u0   grep -q . /etc/hostname (allowed=grep) → PASS.zgrep -q . /etc/hostnamerF   
   r&   PASSr(   r*   r   r+   u3   정상 assertion 명령은 PASS 이어야 함. got=r.   r/   Nr0   r:   s          r    test_case4_normal_passrK   T   s    
#CGW"-F 6V  6V                  >fZH    r"   c                     d} d}t        | |d      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |      rt        j                  |      ndt        j                  |      d	z  }t        j                  d
|      dz   d|iz  }t        t        j                  |            dx}}y)u:   grep -q ZZZNOTFOUND /etc/hostname (allowed=grep) → FAIL.z!grep -q ZZZNOTFOUND /etc/hostnamerF   rI   r&   FAILr(   r*   r   r+   u,   비0 exit 명령은 FAIL 이어야 함. got=r.   r/   Nr0   r:   s          r    test_case5_fail_closedrN   _   s    
-CGW"-F 6V  6V                  7vjA    r"   c                     d} d}t        | |d      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |      rt        j                  |      ndt        j                  |      d	z  }t        j                  d
|      dz   d|iz  }t        t        j                  |            dx}}y)uA   python3 -c 'import time; time.sleep(10)' (timeout=2) → TIMEOUT.z(python3 -c "import time; time.sleep(10)"r$      r&   TIMEOUTr(   r*   r   r+   u4   timeout 초과 명령은 TIMEOUT 이어야 함. got=r.   r/   Nr0   r:   s          r    test_case6_timeout_fail_closedrR   j   s    
6CGW!,F 6Y  6Y                  ?vjI    r"   c                  ~   d} d}t        j                         }t        | |d      }t        j                         |z
  }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      d	z  }t        j                  d
|      dz   d|iz  }t        t        j                  |            dx}}d}||k  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      d	z  }t        j                  d|dd      dz   d|iz  }t        t        j                  |            dx}}y)u_   placeholder 명령 호출이 5초 이내 반환 + SKIP_PLACEHOLDER 출력 (전체 hang 방지).zpython3 ${SLEEP_FOREVER}.pyr$      r&   r'   r(   r*   r   r+   u:   placeholder 명령은 SKIP_PLACEHOLDER 이어야 함. got=r.   r/   Ng      @)<)z%(py0)s < %(py3)selapsedu;   placeholder skip 은 5초 이내 반환해야 함. elapsed=z.2fs)time	monotonicr!   r1   r2   r3   r4   r5   r6   r7   r8   r9   )	r   r   startr   rV   r;   r<   r=   r>   s	            r    $test_case7_placeholder_does_not_hangr[   u   s   
'CGNNEW"-Fnn&G' 6''  6'              (    EVJO      7S=  7S                  Fgc]RST    r"   c                     d} t        j                  dt         j                        }t        j                         5 }t
        j                  j                  |d      }t        |d      5 }|j                  |        ddd       t        |      5 }|j                         }ddd       |j                        }|rq|j                  d      j                         j                  d      }g }|D ];  }	t        j                  d|	      }
|
s|j                  |
j                  d             = ng }ddd       t!              }d	}||k(  }|st#        j$                  d
|fd||f      dt'        j(                         v st#        j*                  t               rt#        j,                  t               nddt'        j(                         v st#        j*                  |      rt#        j,                  |      ndt#        j,                  |      t#        j,                  |      dz  }t#        j.                  d|      dz   d|iz  }t1        t#        j2                  |            dx}x}}y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   OxY w)uQ   ## goal_assertions 섹션이 없는 md 에서 추출 명령 0건 (파싱 회귀).uw   # Task 9999

## description
이 태스크는 goal_assertions 섹션이 없습니다.

## checklist
- [ ] 구현 완료
z'## goal_assertions.*?\n(.*?)(?=\n##|\Z)ztask_no_goals.mdwN   
z	`([^`]+)`r   r(   z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)slencmdsr,   py1r-   py6uP   goal_assertions 섹션 없는 md 에서 추출 명령은 0건이어야 함. got=
>assert %(py8)spy8)recompileStempfileTemporaryDirectoryospathjoinopenwritereadsearchgroupr   splitappendra   r1   r2   r3   r4   r5   r6   r7   r8   r9   )
md_contentpatterntmpdirmd_pathfcontentmlinesrb   line	cmd_matchr;   @py_assert5@py_assert4@py_format7@py_format9s                   r    %test_case8_no_goal_assertions_sectionr      s+   J jj2
G
 
	$	$	& &'',,v'9:'3 	 1GGJ	  '] 	affhG	 NN7#GGAJ$$&,,T2ED 4IIlD9	KK	 234
 D#& t9  9>   9                                [[_Zbc     #	  	 	 	 sB   -I9(I:I9I,A(I9%I9I)	$I9,I6	1I99Jc                  l   t         j                  j                  t              } t	        | d      5 }|j                         }ddd       t        j                  dt        j                        }|j                        }t        |      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  t              rt        j                   t              nddt        j                         v st        j                  |      rt        j                   |      ndt        j                   |      t        j                   |      d	z  }t        j"                  d
|      dz   d|iz  }	t%        t        j&                  |	            dx}x}}y# 1 sw Y   jxY w)ul   테스트 파일 소스에 실제 키 리터럴(16진 8자 이상 특정 패턴)이 없음을 자체 검증.rNz\b[0-9a-f]{16}\br   r(   r`   ra   matchesrc   uN   테스트 파일에 키 리터럴로 보이는 16자 hex 패턴이 발견됨: rf   rg   )rm   rn   abspath__file__rp   rr   rh   ri   
IGNORECASEfindallra   r1   r2   r3   r4   r5   r6   r7   r8   r9   )
test_file_pathr{   sourceforbidden_patternr   r;   r   r   r   r   s
             r    +test_case9_no_raw_key_literals_in_test_filer      sk   WW__X.N	nc	" a 


  ''/Gw< 1 <1   <1                                YY`Xcd      s   F))F3c            	      \   t        j                         5 } t        t        j                  |             }dt        j                  t               dt        j                  |        d}t        j                  dd|gdd| t               t        t        j                  |             }||z
  }t        |      }d}||k(  }|st        j                  d	|fd
||f      dt        j                         v st        j                   t              rt        j"                  t              nddt        j                         v st        j                   |      rt        j"                  |      ndt        j"                  |      t        j"                  |      dz  }t        j$                  d|      dz   d|iz  }	t'        t        j(                  |	            dx}x}}|D ]  }
|
j*                  }d} ||      }| }|st        j$                  d|
       dz   dt        j                         v st        j                   |
      rt        j"                  |
      ndt        j"                  |      t        j"                  |      t        j"                  |      dz  }t'        t        j(                  |            dx}x}x}}|
j*                  }d} ||      }| }|st        j$                  d|
       dz   dt        j                         v st        j                   |
      rt        j"                  |
      ndt        j"                  |      t        j"                  |      t        j"                  |      dz  }t'        t        j(                  |            dx}x}x}} 	 ddd       y# 1 sw Y   yxY w)uX   격리 tempdir 에서 함수 호출 후 신규 파일 0건, .done/.lock 생성 0 검증.r
   z; cd z8; goal_assertion_eval 'grep -q . /etc/hostname' 'grep' 5r   r   Tr   r   r(   r`   ra   	new_filesrc   u:   함수 호출 후 tempdir 에 신규 파일이 생성됨: rf   rg   Nz.doneu   .done 파일 생성됨: zP
>assert not %(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.endswith
}(%(py4)s)
}fname)r,   py2py4re   z.locku   .lock 파일 생성됨: )rk   rl   setrm   listdirr   r   r   r   r   r   ra   r1   r2   r3   r4   r5   r6   r7   r8   r9   endswith)ry   beforer   afterr   r;   r   r   r   r   r   r<   @py_assert3@py_assert7@py_format8s                  r    &test_case10_no_side_effects_in_tempdirr      s   		$	$	& S&RZZ'( -U[[-@,A B++f%& 'EF 	
 	T<(&	
 BJJv&'FN	9~ 	
 	
~" 	
 	
 	
~ 	
 	
	6	
 	
   	
 	
 		  	
 	
	6	
 	
   	
 	
 		  	
 	
 		  	
 	
 		 "# 	
 	
  IV	
 	
 	
 	
 	

  	SE~~RgR~g.R..R.RR2J5'0RRRRRRRuRRRuRRR~RRRgRRR.RRRRRR~~RgR~g.R..R.RR2J5'0RRRRRRRuRRRuRRR~RRRgRRR.RRRRRRR	S5S S Ss   NN""N+c                  >   t        t        d      5 } | j                         }ddd       t        j                  dt        j
                        }d}||u}|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }t        j                  d      d	z   d
|iz  }t        t        j                  |            dx}}|j                  d      }d}||v}|st        j                  d|fd||f      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      nddz  }t        j                  d      d	z   d
|iz  }t        t        j                  |            dx}}d}||v}|st        j                  d|fd||f      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      nddz  }t        j                  d      d	z   d
|iz  }t        t        j                  |            dx}}y# 1 sw Y   rxY w)uc   goal_assertion_eval 함수 본문에 systemctl enable / activation_epoch 가 없음을 grep 확인.r   Nz((goal_assertion_eval\s*\(\)\s*\{.*?\n\}))is not)z%(py0)s is not %(py3)s
func_matchr+   u8   goal_assertion_eval 함수를 소스에서 찾지 못함r.   r/   r^   zsystemctl enable)not in)z%(py1)s not in %(py3)s	func_body)rd   r-   uX   goal_assertion_eval 함수 본문에 'systemctl enable' 가 존재함 (부작용 위험)activation_epochuX   goal_assertion_eval 함수 본문에 'activation_epoch' 가 존재함 (부작용 위험))rp   r   rr   rh   rs   rj   r1   r2   r3   r4   r5   r6   r7   r8   r9   rt   )	r{   r|   r   r;   r<   r=   r>   r   @py_assert0s	            r    .test_case10b_no_systemctl_or_epoch_in_functionr      s   	fc	 a&&(
 3
J
 "]:T!]]]:T]]]]]]:]]]:]]]T]]]#]]]]]]]  #I Y.  Y          &/    &/    	c      Y.  Y          &/    &/    	c    ! s   JJ)rT   )#__doc__builtinsr3   _pytest.assertion.rewrite	assertionrewriter1   rm   rh   r   r   rk   rX   r   environgetrn   ro   r   r   strintr!   r?   rB   rD   rG   rK   rN   rR   r[   r   r   r   r    r"   r    <module>r      s   
  	 	     >	GGLL34
  RC R# RC R R" %R,SBr"   