<commit_before>import findUp from 'find-up';
import * as path from 'path';
import { XmlDocument } from 'xmldoc';
import { readFile } from 'fs-extra';
import { logger } from '../../logger';
import { get } from '../../versioning';
import { PackageDependency, ExtractConfig, PackageFile } from '../common';
import * as semverVersioning from '../../versioning/semver';
import * as datasourceNuget from '../../datasource/nuget';
import { SkipReason } from '../../types';

async function readFileAsXmlDocument(file: string): Promise<XmlDocument> {
  try {
    return new XmlDocument(await readFile(file, 'utf8'));
  } catch (err) {
    logger.debug({ err }, `failed to parse '${file}' as XML document`);
    return undefined;
  }
}

async function determineRegistryUrls(
  packageFile: string,
  localDir: string
): Promise<string[]> {
  const nuGetConfigPath = await findUp('NuGet.config', {
    cwd: path.dirname(path.join(localDir, packageFile)),
    type: 'file',
  });

  if (nuGetConfigPath?.startsWith(localDir) !== true) {
    return undefined;
  }

  logger.debug({ nuGetConfigPath }, 'found NuGet.config');
  const nuGetConfig = await readFileAsXmlDocument(nuGetConfigPath);
  if (!nuGetConfig) {
    return undefined;
  }

  const packageSources = nuGetConfig.childNamed('packageSources');
  if (!packageSources) {
    return undefined;
  }

  const registryUrls = [datasourceNuget.getDefaultFeed()];
  for (const child of packageSources.children) {
    if (child.type === 'element') {
      if (child.name === 'clear') {
        logger.debug(`clearing registry URLs`);
        registryUrls.length = 0;
      } else if (child.name === 'add') {
        let registryUrl = child.attr.value;
        if (child.attr.protocolVersion) {
          registryUrl += `#protocolVersion=${child.attr.protocolVersion}`;
        }
        logger.debug({ registryUrl }, 'adding registry URL');
        registryUrls.push(registryUrl);
      }
    }
  }
  return registryUrls;
}

export async function extractPackageFile(
  content: string,
  packageFile: string,
  config: ExtractConfig = {}
): Promise<PackageFile> {
  logger.trace({ packageFile }, 'nuget.extractPackageFile()');<commit_msg>chore: try to fix coverage<commit_after>import findUp from 'find-up';
import * as path from 'path';
import { XmlDocument } from 'xmldoc';
import { readFile } from 'fs-extra';
import { logger } from '../../logger';
import { get } from '../../versioning';
import { PackageDependency, ExtractConfig, PackageFile } from '../common';
import * as semverVersioning from '../../versioning/semver';
import * as datasourceNuget from '../../datasource/nuget';
import { SkipReason } from '../../types';

async function readFileAsXmlDocument(file: string): Promise<XmlDocument> {
  try {
    return new XmlDocument(await readFile(file, 'utf8'));
  } catch (err) {
    logger.debug({ err }, `failed to parse '${file}' as XML document`);
    return undefined;
  }
}

async function determineRegistryUrls(
  packageFile: string,
  localDir: string
): Promise<string[]> {
  const nuGetConfigPath = await findUp('NuGet.config', {
    cwd: path.dirname(path.join(localDir, packageFile)),
    type: 'file',
  });

  if (nuGetConfigPath?.startsWith(localDir) !== true) {
    return undefined;
  }

  logger.debug({ nuGetConfigPath }, 'found NuGet.config');
  const nuGetConfig = await readFileAsXmlDocument(nuGetConfigPath);
  if (!nuGetConfig) {
    return undefined;
  }

  const packageSources = nuGetConfig.childNamed('packageSources');
  if (!packageSources) {
    return undefined;
  }

  const registryUrls = [datasourceNuget.getDefaultFeed()];
  for (const child of packageSources.children) {
    if (child.type === 'element') {
      if (child.name === 'clear') {
        logger.debug(`clearing registry URLs`);
        registryUrls.length = 0;
      } else if (child.name === 'add') {
        let registryUrl = child.attr.value;
        if (child.attr.protocolVersion) {
          registryUrl += `#protocolVersion=${child.attr.protocolVersion}`;
        }
        logger.debug({ registryUrl }, 'adding registry URL');
        registryUrls.push(registryUrl);
      }
    }
  }
  return registryUrls;
}

export async function extractPackageFile(
  content: string,
  packageFile: string,
  config: ExtractConfig
): Promise<PackageFile> {
  logger.trace({ packageFile }, 'nuget.extractPackageFile()');
{'fraction_non_alphanumeric': 0.14475492441594137, 'fraction_numerical': 0.0009161704076958314, 'mean_word_length': 3.7007534983853607, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7122135', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1286, 'n_words': 327}