<commit_before>
##
# Generic framework for specification testing and linting.
##

##
# Spec testing

SPEC_HISTORY_PATH=~/.spec_history
SPEC_HISTORY_SIZE='100'

spec() {
	local args=$(args_and_or_stdin $@ <&0)
	print_spec_header ${args}
	write_spec_history ${args}
	run_spec_command ${args}
}

write_spec_history() {
	local new_entry="$@"
	local prev_entry=$(read_spec_history 1 2>/dev/null)
	if [[ "${new_entry}" == "${prev_entry}" ]]; then
		return 0
	fi
	echo "${new_entry}" >> $SPEC_HISTORY_PATH

	# Hacky file rotation
	local temp_path="${SPEC_HISTORY_PATH}.tmp"
	tail -n "$SPEC_HISTORY_SIZE" "$SPEC_HISTORY_PATH" > "${temp_path}"
	mv "${temp_path}" "$SPEC_HISTORY_PATH"
}

read_spec_history() {
	local range_end=$1
	local spec_history

	local out_path=$(mktemp)
	trap "rm ${out_path}" RETURN

	if empty $range_end; then
		tail -n 1 $SPEC_HISTORY_PATH > ${out_path}

	elif [[ $range_end =~ [0-9]+ ]]; then
		tail -n $range_end $SPEC_HISTORY_PATH > ${out_path}

	elif [[ $range_end =~ all ]]; then
		cat $SPEC_HISTORY_PATH > ${out_path}
	fi

	if [[ ! -s ${out_path} ]]; then
		echo_error 'No spec history!'
		return $YA_DUN_GOOFED
	fi

	cat ${out_path}
}

echo_paths() {
	echo 'Found these paths:'
	ls -1 $@
}

respec() {
	local spec_history=$(read_spec_history)
	if [[ $? > 0 ]]; then
		return $YA_DUN_GOOFED
	fi

	echo 'Replaying spec:' $spec_history $@
	spec $spec_history $@
}

globspec() {
	check_help $@ && globspec_help && return 0

	if empty $@; then
		echo_error 'Error: must provide a pattern!'
		echo $usage<commit_msg>spec_history<commit_after>
##
# Generic framework for specification testing and linting.
##

##
# Spec testing

SPEC_HISTORY_PATH=~/.spec_history
SPEC_HISTORY_SIZE='100'

spec() {
	local args=$(args_and_or_stdin $@ <&0)
	print_spec_header ${args}
	write_spec_history ${args}
	run_spec_command ${args}
}

write_spec_history() {
	local new_entry="$@"
	local prev_entry=$(spec_history 1 2>/dev/null)
	if [[ "${new_entry}" == "${prev_entry}" ]]; then
		return 0
	fi
	echo "${new_entry}" >> $SPEC_HISTORY_PATH

	# Hacky file rotation
	local temp_path="${SPEC_HISTORY_PATH}.tmp"
	tail -n "$SPEC_HISTORY_SIZE" "$SPEC_HISTORY_PATH" > "${temp_path}"
	mv "${temp_path}" "$SPEC_HISTORY_PATH"
}

spec_history() {
	local range_end="$1"

	local out_path=$(mktemp)
	trap "rm ${out_path}" RETURN

	if [[ ${range_end} =~ [0-9]+ ]]; then
		tail -n ${range_end} $SPEC_HISTORY_PATH > ${out_path}
	elif [[ -z ${range_end} || ${range_end} =~ all ]]; then
		cat $SPEC_HISTORY_PATH > ${out_path}
	else
		echo_error "Invalid history range: '${range_end}'"
	fi

	if [[ ! -s ${out_path} ]]; then
		echo_error 'No spec history!'
		echo_error "Expected it to be present in: '$SPEC_HISTORY_PATH'"
		return $YA_DUN_GOOFED
	fi

	cat ${out_path}
}

echo_paths() {
	echo 'Found these paths:'
	ls -1 $@
}

respec() {
	local last_spec=$(spec_history 1)
	if [[ $? > 0 ]]; then
		return $YA_DUN_GOOFED
	fi

	echo 'Replaying spec:' $last_spec $@
	spec $last_spec $@
}

globspec() {
	check_help $@ && globspec_help && return 0

	if empty $@; then
		echo_error 'Error: must provide a pattern!'
		echo $usage
{'fraction_non_alphanumeric': 0.15811552113585028, 'fraction_numerical': 0.0090351726363343, 'mean_word_length': 4.7835820895522385, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2453030', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1217, 'n_words': 285}