<commit_before>/*
 * Copyright 2017 flow.ci
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flow.platform.cmd;

import com.flow.platform.domain.CmdResult;
import com.flow.platform.util.DateUtil;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.io.*;
import java.lang.reflect.Field;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author <EMAIL>
 */
public final class CmdExecutor {

    /**
     * Class for null proc listener
     */
    private final class NullProcListener implements ProcListener {
        @Override
        public void onStarted(CmdResult result) {

        }

        @Override
        public void onLogged(CmdResult result) {

        }

        @Override
        public void onExecuted(CmdResult result) {

        }

        @Override
        public void onException(CmdResult result) {
        }
    }

    /**
     * Class for null log listener
     */
    private final class NullLogListener implements LogListener {
        @Override
        public void onLog(Log log) {

        }

        @Override
        public void onFinish() {

        }
    }

    private final static File DEFAULT_WORKING_DIR = new File(System.getProperty("user.home"));

    private final CountDownLatch logLath = new CountDownLatch(1);
    private final ConcurrentLinkedQueue<Log> loggingQueue = new ConcurrentLinkedQueue<>();

    private final AtomicInteger stdCounter = new AtomicInteger(2); // std, stderr
    private final String endTerm = String.format("=====EOF-%s=====", UUID.randomUUID());
    private final int bufferSize = 1024 * 1024 * 5; // 5 mb buffer

    private ProcessBuilder pBuilder;
    private ProcListener procListener = new NullProcListener();
    private LogListener logListener = new NullLogListener();
    private Integer timeout = new Integer(3600 * 2); // process timeout in seconds, default is 2 hour
    private List<String> cmdList;
    private String outputEnvFilter;
    private CmdResult outputResult;

    /**
     * @param procListener    nullable
     * @param logListener     nullable
     * @param inputs          nullable input env
     * @param workingDir      nullable, for set cmd working directory, default is user.dir
     * @param outputEnvFilter nullable, for start_with env to cmd result output
     * @param cmd             exec cmd
     * @throws FileNotFoundException throw when working dir defined but not found
     */
    public CmdExecutor(final ProcListener procListener,
                       final LogListener logListener,
                       final Map<String, String> inputs,
                       final String workingDir,
                       final String outputEnvFilter,
                       final Integer timeout,
                       final String... cmd) throws FileNotFoundException {

        if (procListener != null) {
            this.procListener = procListener;
        }

        if (logListener != null) {
            this.logListener = logListener;
        }

        this.outputEnvFilter = "FLOW";
        cmdList = Lists.newArrayList(cmd);
        pBuilder = new ProcessBuilder("/bin/bash");
<commit_msg>fix unit test in cmd runner<commit_after>/*
 * Copyright 2017 flow.ci
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flow.platform.cmd;

import com.flow.platform.domain.CmdResult;
import com.flow.platform.util.DateUtil;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.io.*;
import java.lang.reflect.Field;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author <EMAIL>
 */
public final class CmdExecutor {

    /**
     * Class for null proc listener
     */
    private final class NullProcListener implements ProcListener {
        @Override
        public void onStarted(CmdResult result) {

        }

        @Override
        public void onLogged(CmdResult result) {

        }

        @Override
        public void onExecuted(CmdResult result) {

        }

        @Override
        public void onException(CmdResult result) {
        }
    }

    /**
     * Class for null log listener
     */
    private final class NullLogListener implements LogListener {
        @Override
        public void onLog(Log log) {

        }

        @Override
        public void onFinish() {

        }
    }

    private final static File DEFAULT_WORKING_DIR = new File(System.getProperty("user.home"));

    private final CountDownLatch logLath = new CountDownLatch(1);
    private final ConcurrentLinkedQueue<Log> loggingQueue = new ConcurrentLinkedQueue<>();

    private final AtomicInteger stdCounter = new AtomicInteger(2); // std, stderr
    private final String endTerm = String.format("=====EOF-%s=====", UUID.randomUUID());
    private final int bufferSize = 1024 * 1024 * 5; // 5 mb buffer

    private ProcessBuilder pBuilder;
    private ProcListener procListener = new NullProcListener();
    private LogListener logListener = new NullLogListener();
    private Integer timeout = new Integer(3600 * 2); // process timeout in seconds, default is 2 hour
    private List<String> cmdList;
    private String outputEnvFilter;
    private CmdResult outputResult;

    /**
     * @param procListener    nullable
     * @param logListener     nullable
     * @param inputs          nullable input env
     * @param workingDir      nullable, for set cmd working directory, default is user.dir
     * @param outputEnvFilter nullable, for start_with env to cmd result output
     * @param cmd             exec cmd
     * @throws FileNotFoundException throw when working dir defined but not found
     */
    public CmdExecutor(final ProcListener procListener,
                       final LogListener logListener,
                       final Map<String, String> inputs,
                       final String workingDir,
                       final String outputEnvFilter,
                       final Integer timeout,
                       final String... cmd) throws FileNotFoundException {

        if (procListener != null) {
            this.procListener = procListener;
        }

        if (logListener != null) {
            this.logListener = logListener;
        }

        this.outputEnvFilter = outputEnvFilter;
        cmdList = Lists.newArrayList(cmd);
        pBuilder = new ProcessBuilder("/bin/bash");

{'fraction_non_alphanumeric': 0.08015122873345935, 'fraction_numerical': 0.006553245116572149, 'mean_word_length': 2.6504139834406626, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '140842', 'n_tokens_mistral': 2066, 'n_tokens_neox': 1963, 'n_words': 677}