<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you 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 org.apache.calcite.adapter.os;

import org.apache.calcite.config.CalciteConnectionProperty;
import org.apache.calcite.config.Lex;
import org.apache.calcite.runtime.Hook;
import org.apache.calcite.sql.validate.SqlConformanceEnum;
import org.apache.calcite.test.CalciteAssert;
import org.apache.calcite.util.Holder;
import org.apache.calcite.util.Util;

import com.google.common.base.Function;

import org.hamcrest.CoreMatchers;
import org.junit.Assume;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.SQLException;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

/**
 * Unit tests for the OS (operating system) adapter.
 *
 * <p>Also please run the following tests manually, from your shell:
 *
 * <ul>
 *   <li>./sqlsh select \* from du
 *   <li>./sqlsh select \* from files
 *   <li>./sqlsh select \* from git_commits
 *   <li>./sqlsh select \* from ps
 *   <li>(echo cats; echo and dogs) | ./sqlsh select \* from stdin
 *   <li>./sqlsh select \* from vmstat
 * </ul>
 */
public class OsAdapterTest {
  private static boolean isWindows() {
    return System.getProperty("os.name").startsWith("Windows");
  }

  /** Returns whether there is a ".git" directory in this directory or in a
   * directory between this directory and root. */
  private static boolean hasGit() {
    final String path = OsAdapterTest.class.getResource("/").getPath();
    File f = new File(path);
    for (;;) {
      if (f == null || !f.exists()) {
        return false; // abandon hope
      }
      File[] files =
          f.listFiles(
              new FilenameFilter() {
                public boolean accept(File dir, String name) {
                  return name.equals(".git");
                }
              });
      if (files != null && files.length == 1) {
        return true; // there is a ".git" subdirectory
      }
      f = f.getParentFile();
    }
  }

  @Test public void testDu() {
    sql("select * from du")
        .returns(
            new Function<ResultSet, Void>() {
              public Void apply(ResultSet r) {
                try {
                  assertThat(r.next(), is(true));
                  assertThat(r.getInt(1), notNullValue());
                  assertThat(r.getString(2), CoreMatchers.startsWith("./"));
                  assertThat(r.wasNull(), is(false));
                  return null;
                } catch (SQLException e) {
                  throw new RuntimeException(e);
                }
              }
            });
  }

  @Test public void testDuFilterSortLimit() {
    sql("select * from du where path like '%/src/test/java/%'\n"
        + "order by 1 limit 2")
        .returns(
            new Function<ResultSet, Void>() {
              public Void apply(ResultSet r) {
                try {
                  assertThat(r.next(), is(true));
                  assertThat(r.getInt(1), notNullValue());<commit_msg>[CALCITE-2369] Fix OsAdapterTest failure on windows (<NAME>)<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you 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 org.apache.calcite.adapter.os;

import org.apache.calcite.config.CalciteConnectionProperty;
import org.apache.calcite.config.Lex;
import org.apache.calcite.runtime.Hook;
import org.apache.calcite.sql.validate.SqlConformanceEnum;
import org.apache.calcite.test.CalciteAssert;
import org.apache.calcite.util.Holder;
import org.apache.calcite.util.Util;

import com.google.common.base.Function;

import org.hamcrest.CoreMatchers;
import org.junit.Assume;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.SQLException;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

/**
 * Unit tests for the OS (operating system) adapter.
 *
 * <p>Also please run the following tests manually, from your shell:
 *
 * <ul>
 *   <li>./sqlsh select \* from du
 *   <li>./sqlsh select \* from files
 *   <li>./sqlsh select \* from git_commits
 *   <li>./sqlsh select \* from ps
 *   <li>(echo cats; echo and dogs) | ./sqlsh select \* from stdin
 *   <li>./sqlsh select \* from vmstat
 * </ul>
 */
public class OsAdapterTest {
  private static boolean isWindows() {
    return System.getProperty("os.name").startsWith("Windows");
  }

  /** Returns whether there is a ".git" directory in this directory or in a
   * directory between this directory and root. */
  private static boolean hasGit() {
    final String path = OsAdapterTest.class.getResource("/").getPath();
    File f = new File(path);
    for (;;) {
      if (f == null || !f.exists()) {
        return false; // abandon hope
      }
      File[] files =
          f.listFiles(
              new FilenameFilter() {
                public boolean accept(File dir, String name) {
                  return name.equals(".git");
                }
              });
      if (files != null && files.length == 1) {
        return true; // there is a ".git" subdirectory
      }
      f = f.getParentFile();
    }
  }

  @Test public void testDu() {
    Assume.assumeFalse("Skip: the 'du' table does not work on Windows",
        isWindows());
    sql("select * from du")
        .returns(
            new Function<ResultSet, Void>() {
              public Void apply(ResultSet r) {
                try {
                  assertThat(r.next(), is(true));
                  assertThat(r.getInt(1), notNullValue());
                  assertThat(r.getString(2), CoreMatchers.startsWith("./"));
                  assertThat(r.wasNull(), is(false));
                  return null;
                } catch (SQLException e) {
                  throw new RuntimeException(e);
                }
              }
            });
  }

  @Test public void testDuFilterSortLimit() {
    Assume.assumeFalse("Skip: the 'du' table does not work on Windows",
        isWindows());
    sql("select * from du where path like '%/src/test/java/%'\n"
        + "order by 1 limit 2")
        .returns(
            new Function<ResultSet, Void>() {
              public Void apply(ResultSet r) {
                try {
                  assertThat(r.next(), is(true));
                  assertThat(r.getInt(1), notNullValue());
{'fraction_non_alphanumeric': 0.11326784234820689, 'fraction_numerical': 0.002840572848857853, 'mean_word_length': 2.6172945205479454, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '7059946', 'n_tokens_mistral': 2385, 'n_tokens_neox': 2328, 'n_words': 739}